Forum

> > CS2D > Scripts > load points only if player had saved before
Forums overviewCS2D overview Scripts overviewLog in to reply

English load points only if player had saved before

4 replies
To the start Previous 1 Next To the start

old load points only if player had saved before

khaled1968
User Off Offline

Quote
This is a script I used to save player points...

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
savestr={}

function readfile(filelocation, linenum) 
saveline=1
for line in io.lines(filelocation) do 
savestr[saveline]=line
saveline=saveline+1
end
return savestr[linenum]
end

addhook("join" ,"playerjoin")
function playerjoin(id)
if player(id,"usgn") ~= 0 then
savedpoints = readfile("sys/lua/autorun/savefile/"..player(id,"usgn")..".txt", 1)-1+1
points[id] = savedpoints
msg2(id,"\169000255000Your points was succesfully loaded")
end

addhook("leave","saveleave")
function saveleave(id)
if player(id,"usgn") ~= 0 then
local c = points[id]
ppoints = io.open("sys/lua/autorun/savefile/"..player(id,"usgn")..".txt", "w")
ppoints:write(""..c.."")
ppoints:close()
end
end

However, It worked! but the only problem I faced is that when a usgn user join the server and he didn't save before, the script will not work well and some bugs will appear.

So... I want to know how to make the load system work ONLY if the player usgn exists at the savefile in the first place?
edited 1×, last 08.08.22 01:43:29 pm

old Re: load points only if player had saved before

Cure Pikachu
User Off Offline

Quote
I think the call here is doing some tweaks to the custom
readfile
function you have there to return
nil
if the file does not exist then work from there.
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
points = {}

function readfile(filelocation,linenum)
	local savestr = {}
	local file = io.open(filelocation,"r")
	if file then
		local saveline=1
		for line in file:lines() do 
			savestr[saveline]=line
			saveline=saveline+1
		end
		file:close()
		return savestr[linenum]
	end
	return nil
end

addhook("join","playerjoin")
function playerjoin(id)
	if player(id,"usgn") ~= 0 then
		local x = readfile("sys/lua/autorun/savefile/"..player(id,"usgn")..".txt",1)
		if x ~= nil then
			points[id] = tonumber(x)
			msg2(id,"\169000255000Your points was succesfully loaded")
		else
			points[id] = 0
			msg2(id,"\169255000000Loading points failed, resetting...")
		end
	end
end

addhook("leave","saveleave")
function saveleave(id)
	if player(id,"usgn") ~= 0 then
		local ppoints = io.open("sys/lua/autorun/savefile/"..player(id,"usgn")..".txt","w")
		ppoints:write(points[id])
		ppoints:close()
	end
end
edited 4×, last 13.08.22 12:37:57 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview