Forum

> > CS2D > Scripts > [Solved]Save/load multiple value
Forums overviewCS2D overview Scripts overviewLog in to reply

English [Solved]Save/load multiple value

11 replies
To the start Previous 1 Next To the start

old [Solved]Save/load multiple value

Ace Howl
User Off Offline

Quote
Hello.
I have two problems (related with the title)...
1 - I do not remember how to load the values. Plus, how to load more than one value?
1
2
3
4
5
6
7
8
9
function svJoin(id)
	if (player(id,"usgn") > 0) then
		local file = io.open ("sys/lua/USGN/"..player(id,"usgn")..".txt","r")
		if (file) then
			file:read () <--- ???
			file:close ()
		end
	end
end
Note: Don't mind the arrow, it just want to guide you the problem.

Those values are Kill[id],Dead[id] and KDR[id].

2 - Any way to make content in saved folder has separated (neat) text?

1
2
3
4
5
6
7
8
This is "..player(id,"usgn")..".txt
--------------------------------------------------
Name: "..player(id,"name").."
Kill: "..Kill[id].."
Deaths: "..Dead[id].."
etc
etc
etc
While this is the save script:
1
2
3
4
5
6
7
function svLeave(id)
	if (player(id,"usgn")> 0) then
		local file = io.open ("sys/luaUSGN/"..player(id,"usgn")..".txt","w")
		file:write ("Name: "..player(id,"name").." | Kill: "..Kill[id].." | Deaths: "..Dead[id])
		file:close ()
	end
end
For now, I just use the '|' as separator.

Any solutions are welcome.
edited 1×, last 31.10.14 06:46:55 am

old Re: [Solved]Save/load multiple value

Yates
Reviewer Off Offline

Quote
My save and load functions. Simplified for beginner edit, copy & use. This script uses 2 values as an example.
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

old Re: [Solved]Save/load multiple value

Ace Howl
User Off Offline

Quote
@user Yates, your solution is useful, but...
Console has written
LUA ERROR: sys/lua/goFolder/goScript.lua:15: attempt to index local 'rKill' (a number value)

This is the part of code if you want to view and fix it.
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
rKill = {}
rDead = {}
MVP = {}

for id = 1,32 do rKill[id] = 0 end
for id = 1,32 do rDead[id] = 0 end
for id = 1,32 do MVP[id] = 0 end

addhook("join","goJoin",2)
function goJoin(id) -- Load profile
	if (player(id,"usgn") > 0) then
		local file = io.open ("sys/lua/goFolder/goUSGN/"..player(id,"usgn")..".txt","r")
		if file then
			local rKill, rDead, MVP = file:read("*n","*n","*n")
			rKill[id] = rKill
			rDead[id] = rDead
			MVP[id] = MVP
			file:close ()
			msg2(id,"\169255255255Profile successfully loaded.")
		end
	end
end

addhook("leave","goLeave")
function goLeave(id)
	if (player(id,"usgn") > 0) then
		local file = io.open ("sys/lua/goFolder/goUSGN/"..player(id,"usgn")..".txt","w")
		local rKill, rDead, MVP = rKill[id], rDead[id], MVP[id]
		file:write (rKill.."\n"..rDead.."\n"..MVP)
		file:close ()
	end
	rKill[id] = 0
	rDead[id] = 0
	MVP[id] = 0
end

old Re: [Solved]Save/load multiple value

Yates
Reviewer Off Offline

Quote
Rename the local values:
1
2
3
4
local lrKill, lrDead, lMVP = file:read("*n","*n","*n")
rKill[id] = lrKill
rDead[id] = lrDead
MVP[id] = lMVP
I just put an l infront of them for local. Change them to whatever you want if this solution works.

I think it's because you have a table called rKill and are trying to create a local value with the same name and use it as a number value.

And if this works, change the save script as well..

old [Solved] oh.. wait..!

Ace Howl
User Off Offline

Quote
This is exactly what I need. Now, your code are now working. Thank you for your help, user Yates!

Wait! Somehow this thing showed up...
1
2
3
4
5
6
7
8
9
10
11
function goLeave(id)
	if (player(id,"usgn") > 0) then
		local file = io.open ("sys/lua/goFolder/goUSGN/"..player(id,"usgn")..".txt","w")
		local sKill, sDead, sMVP = rKill[id], rDead[id], MVP[id]
		file:write (sKill.."\n"..sDead.."\n"..sMVP)
		file:close ()
	end
	rKill[id] = 0
	rDead[id] = 0
	MVP[id] = 0
end
Console has written
LUA ERROR: sys/lua/goFolder/goScript.lua:5: attempt to concatenate local 'sMVP' (a nil value)
edited 1×, last 30.10.14 04:00:02 pm

old Re: [Solved]Save/load multiple value

Rainoth
Moderator Off Offline

Quote
1
2
local sKill, sDead, sMVP = rKill[id], rDead[id], MVP[id]
file:write (sKill.."\n"..sDead.."\n"..sMVP)

It can't concatenate sMVP because it's equal to nothing (doesn't exist).
1
sMVP = MVP[id]
MVP[id] has no value. Check if you set the value correctly and if you don't, that's your problem.

old Re: [Solved]Save/load multiple value

Ace Howl
User Off Offline

Quote
I tried to remove the MVP[id], the problem still appears (assume at the end of the sMVP was sDead, Dead[id] also error in this case, following sKill). There has something that I doesn't need to edit leave function, which was working...

old Re: [Solved]Save/load multiple value

Yates
Reviewer Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
function goLeave(id)
     --[[if (player(id,"usgn") > 0) then
          local file = io.open ("sys/lua/goFolder/goUSGN/"..player(id,"usgn")..".txt","w")]]--
          local sKill, sDead, sMVP = rKill[id], rDead[id], MVP[id]
		print(sKill.."-"..rKill[id])
          --[[file:write (sKill.."\n"..sDead.."\n"..sMVP)
          file:close ()
     end]]--
     rKill[id] = 0
     rDead[id] = 0
     MVP[id] = 0
end
Can you tell us what error is shown on print? (If any).

old [Solved] Oops...

Ace Howl
User Off Offline

Quote
Oh no... I've discovered that the only successful way to load values are by through:
1
2
3
4
5
6
7
8
9
10
function load(id)
	if (player(id,"usgn") > 0 then
		local file = io.open ("directory/"..player(id,"usgn")..".txt","r")
		if file then
			value[id] = file:read("*n") <---
			value2[id] = file:read(*n") <---
			file:close ()
		end
	end
end
Those pointed variables are the only reliable...
Finally, thanks to you guys for helping me especially user Yates

@user Yates: The error that I'd realise was the way to save the file.
1
2
3
local 1a, 1b, 1c = a[id], b[id] c[id]
file:write (1a.."\n"..1b.."\n"..1c)
file:close
There was no error in the script. The only problem is the way to save it.

old Re: [Solved]Save/load multiple value

Yates
Reviewer Off Offline

Quote
Well it worked without changing to those exact values so it was probably an error in setting/naming them the same as something else but if it works for you glad to help out.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview