Forum

> > CS2D > Scripts > Attempt to index local file (a nil value)
Forums overviewCS2D overview Scripts overviewLog in to reply

English Attempt to index local file (a nil value)

5 replies
To the start Previous 1 Next To the start

old Attempt to index local file (a nil value)

Talented Doge
User Off Offline

Quote
I've tried to read through every forum thread(if I haven't missed) that might help me, but I failed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
dir = "/sys/lua/data/"
defclr = 012255012
on = {}
USGN = {}
ranking = {}
saycolor = {}

function loaduser(id)
	local file = io.open (dir..USGN[id]..".txt")
	if file then
		local ranking_f,saycolor_f = file:read ("*n","*n")
		ranking[id] = ranking_f or 1
		saycolor[id] = saycolor_f or 255255000
		file:close()
	else
		ranking[id] = 1
		saycolor[id] = 255255000
		local ranking_f = ranking[id]
		local saycolor_f = saycolor[id]
		file:write(ranking_f.."\n"..saycolor_f)
		file:close()
	end
end

function welcomemsg(id, md)
	if md == 1 then
		msg2 (id, "\169"..defclr.."Welcome to"..game("sv_name")"!")
	end
end

function saveuser(id)
	local file = io.open (dir..USGN[id]..".txt")
	local ranking_f = ranking[id]
	local saycolor_f = saycolor[id]
	file:write(ranking_f.."\n"..saycolor_f)
	file:close()
end	

addhook("join", "on.join")
function on.join(id)
	USGN[id] = player(id, "usgn")
	if USGN[id] > 0 then 
		loaduser(id)
	else
		welcomemsg(id, 1)
	end
end

addhook("leave", "on.leave")
function on.leave(id)
	if USGN[id] > 0 then
		saveuser(id)
	end
end

I attempted to create a file using users' USGN id as file name.
But it returns me
1
LUA ERROR: sys/lua/autorun/new  1.lua:20: attempt to index local 'file' (a nil value)

I wrote this file according to Rainoth.

old Re: Attempt to index local file (a nil value)

G3tWr3ck3d
User Off Offline

Quote
I am not sure what you are trying to do with that code, but. I suggest you to use something easier (for newbies). So that you can understand what you're trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function _save(id)
     local usgn = player(id,"usgn")
     if usgn > 0 then
          local file = io.open("sys/lua/<yourfilelocation>/"..usgn..".txt","w")
          local value1, value2 = playervalue1, playervalue2
          file:write(value1.."\n"..value2)
          print("©000255000Saved.")
          file:close()
     end
end

function _load(id)
     local usgn = player(id,"usgn")
     if usgn > 0 then
          local file = io.open("sys/lua/<yourfilelocation>/"..usgn..".txt","r")
          if file then
               -- Save file found, read .txt file and set player values
               local value1, value2 = file:read("*n","*n")
               playervalue1 = value1
               playervalue2 = value2
               file:close()
          else
               -- Set player values to 0, save file not found
               <playervalue> = 0
               <playervalue> = 0
          end
     else
          -- No USGN ID found, set player values to 0
          Player[id].money = 0
          Player[id].registration = 0
     end
     -- You may put a function here to generate hudtxt for example
end

Here you go, use this instead of yours.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview