Forum

> > CS2D > Scripts > Saving Script
Forums overviewCS2D overview Scripts overviewLog in to reply

English Saving Script

18 replies
To the start Previous 1 Next To the start

old Saving Script

Louie
User Off Offline

Quote
I want to create a Level system,but my problem is how do i make a save system that automatically saves the users Level and Exp when they exit,and automatically loads their Level and exp when they join?

old Re: Saving Script

KimKat
GAME BANNED Off Offline

Quote
You need to learn Lua to be able to do that.

Once you studied Lua a bit you will figure it out. I did and I can create the thing you're requesting easily.

However, I go by the chinese proverb "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime."

I hope you get the point I'm making. us

To give you some advice.

1. Learn to store values into variables or even tables.
2. Learn to use/display the variables and tables in in-game hudtexts.
3. Experiment around with the hooks and try to be creative.
4. Gain some more experience in Lua scripting by reading tutorials either on YouTube or on the Lua official website.

old Re: Saving Script

Rainoth
Moderator Off Offline

Quote
OR, you could do what everyone else does and use the work of someone else

Check these scripts out:
file File does not exist (14215)
file File does not exist (12888)

OR, you could search the forum and find many more level scripts that you could use. It's that easy

If you wanted to do something on the computer, why didn't you try to make your own OS. Wouldn't that be great? Yet neither of us did because people like to use the work of others.

However, if you want to have something amazing only for YOURSELF, you'll have to follow user KimKat's advice and learn Lua yourself. I highly doubt you'll need that as there's many great scripts out there.

Good luck!

old Re: Saving Script

Louie
User Off Offline

Quote
I think i'll try to learn it ,its more fun to learn it and then do it myself Thx guys for answering.
EDIT: Ok so i have created an load/save script.
√ Yeah it loads and saves but,
× when i delete my server(dedicated server) then start a new one it doesn't load the file,the gold,level,exp just goes back to 0 wtf? heres my code.
Spoiler >
edited 2×, last 01.02.15 01:36:33 pm

old Re: Saving Script

Louie
User Off Offline

Quote
@user Ajmin: Yeah it helped me understand more and i tried to follow what you did there to use
1
usgn[id] = player(id,'usgn')
though its still the same when i close the server then make another one it doesn't load

old Re: Saving Script

Ajmin
User Off Offline

Quote
1
2
3
"playervalue1" --It seems that u aint gave any value for this.
So use:
"value1"

old Re: Saving Script

Louie
User Off Offline

Quote
user Ajmin has written
1
2
3
"playervalue1" --It seems that u aint gave any value for this.
So use:
"value1"

I did not get it

old Re: Saving Script

Raaj Nadar
User Off Offline

Quote
you need to add a name to the players saved value and use the same value while loading.

old Re: Saving Script

Louie
User Off Offline

Quote
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
addhook("leave","saveleave")
function saveleave(id)
     if usgn[id] > 0 then
          local file = io.open("sys/lua/MoneyLevel Sys/Save/"..usgn..".txt","w")
          local value1, value2, value3, value4 = gold[id], Levels[id], Exp[id], MaxExp[id]
          file:write(value1.."\n "..value2.."\n "..value3.."\n"..value4)
          file:close()
     end
end

addhook("join","joinload")
function joinload(id)
     if usgn[id] > 0 then
          local file = io.open("sys/lua/MoneyLevel Sys/Save/"..usgn..".txt","r")
          if file then
               local value1, value2 = file:read("*n","*n","*n","*n")
               value1 = gold[id]
               value2 = Levels[id]
               value3 = Exp[id]
               value4 = MaxExp[id]
               msg2(id, "©000255000Succesfully Loaded!")
               file:close()
          else
               -- Set player values to 0, save file not found
               gold[id] = 0
               Levels[id] = 0
               Exp[id] = 0
               MaxExp[id] = 350
               msg2(id, "©255000000Failed to Load!")
          end
     else
          -- No USGN ID found, set player values to 0
               gold[id] = 0
               Levels[id] = 0
               Exp[id] = 0
               MaxExp[id] = 350
               msg2(id, "©255000000No USGN found!")
     end
     
end
√ it saves/loads files
× when i close the server and make/start a new one it doesn't load the files.

old Re: Saving Script

Yates
Reviewer Off Offline

Quote
Load a player on first spawn or team change. Never load on join.

You can use a variable to check if someone has spawned for the first time and set it to 1 so it doesn't trigger load again.

old Re: Saving Script

Louie
User Off Offline

Quote
@user Yates: Ok i'll have to try that
EDIT: I've been getting these errors, somethingl like attempt to concatenate '?' (a nil value) and i also get attempt to concatenate global 'usgn' (a nil value)
though i don't know what those errors mean ,help please.
edited 1×, last 05.02.15 07:24:44 am

old Re: Saving Script

Bowlinghead
User Off Offline

Quote
The compiler also shows the line where the error is. Is everything right in this line? You maybe forgot a charakter like ")" or something.
Maybe it is because you take "usgn" as variable name because it is already reserved.

old Re: Saving Script

Rainoth
Moderator Off Offline

Quote
If that's your full script, you're missing the tables themselves.

old Re: Saving Script

Ajmin
User Off Offline

Quote
See this Error!
1
local file = io.open("sys/lua/MoneyLevel Sys/Save/"..usgn..".txt","w")

The correct one:
1
local file = io.open("sys/lua/MoneyLevel Sys/Save/"..usgn[id]..".txt","w")

You coded "usgn" instead of "usgn[id]" !!

Edit:
Completely fixed script:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function initArray(m)
	local array = {}
		for i = 1, m do
			array[i]=0
		end
return array
end

function string.split(text,b)
local cmd = {}
if b then
b = b
else
b = "%s"
end
b = "[^"..b.."]+"
for o in string.gmatch(text,b) do
table.insert(cmd,o)
end
return cmd
end



gold =initArray(32)
Levels=initArray(32)
Exp=initArray(32)
MaxExp=initArray(32)
usgn=initArray(32)


addhook("join","joinload")
function joinload(id)
usgn[id]=player(id,"usgn")
     if usgn[id] > 0 then
	 if Exist("sys/lua/MoneyLevel Sys/Save/"..usgn[id]..".txt") then
          for line in io.lines("sys/lua/MoneyLevel Sys/Save/"..usgn[id]..".txt") do
               line = line:split()
local value1 = tonumber(line[1])
				local value2 = tonumber(line[2])
				local value3 = tonumber(line[3])
                                local value4 = tonumber(line[4])
               gold[id]=value1
               Levels[id]=value2 
              Exp[id]=value3
               MaxExp[id]=value4
               msg2(id, "©000255000Succesfully Loaded!")
              end
          else
               -- Set player values to 0, save file not found
               gold[id] = 0
               Levels[id] = 0
               Exp[id] = 0
               MaxExp[id] = 350
               msg2(id, "©255000000Save Fail Not found!")
          end
     else
          -- No USGN ID found, set player values to 0
               gold[id] = 0
               Levels[id] = 0
               Exp[id] = 0
               MaxExp[id] = 350
               msg2(id, "©255000000No USGN found!")
     end
     
end

addhook("ms100","hud")
function hud()
     for id = 1,32 do  
               parse('hudtxt2 '..id..' 1 "©000177255Gold :: '..gold[id]..'" 13 120 0')
               parse('hudtxt2 '..id..' 2 "©000255150Level :: '..Levels[id]..' " 13 140 0')
	       parse('hudtxt2 '..id..' 5 "©255255000EXP :: '..Exp[id]..' / '..MaxExp[id]..'" 13 160 0')       
     end
end






addhook("leave","saveleave")
function saveleave(id)
     if usgn[id] > 0 then
          io.output(io.open("sys/lua/MoneyLevel Sys/Save/"..usgn[id]..".txt","w+"))
          io.write(gold[id].."  "..Levels[id].." "..MaxExp[id].." "..Exp[id])
          io.close()
		msg2(id,"©000255000Succesfully Saved!")
	else
		msg2(id,"©255000000Failed to save, you're not logged in")
     end
end

function Exist(location)
     local f=io.open(location)
     if f~=nil then
          io.close(f)
     end
     return f~=nil
end

Its fully guaranteed script!
It will never give any error!
Even if i use "Join" and "Leave" hook!
It wont
edited 2×, last 05.02.15 11:41:09 am

old Re: Saving Script

Louie
User Off Offline

Quote
@user Ajmin: Yea it loads and saves but it only loads when i:
1. Join server(dedicated server)
2. Get a few kills to lvl up
3. leave
4. rejoin
5. the files get loaded
however if i do:
1 Join server
2. Get a few kills to lvl up
3. leave and close down server
4. create server again
5. doesn't load files if i do that way.
but at least im not getting any error messages in the console √

old Re: Saving Script

Ajmin
User Off Offline

Quote
Yeh its a simple error! (Actually We cannot treat it as a error!)

Shutting down server is not leave (Leave == Save)!
Hence,It wont save!

U must use "second" save or "minute" save for that!
Or simply save players lvl etc when it gets increased. (Best Way)

Else u could also add a cmd for save function!
When u need to shutdown your server type the cmd and then shutdown it.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview