English load points only if player had saved before

4 replies
Goto Page
To the start Previous 1 Next To the start
Up
khaled1968
User
Offline Off
This is a script I used to save player points...

Code:
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
08.08.22 02:41:37 pm
Up
Cure Pikachu
User
Offline Off
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.
Code:
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
IMG:https://i.imgur.com/DeSeUxC.png
IMG:https://i.imgur.com/xpsyQRX.png
10.08.22 04:01:27 pm
Up
khaled1968
User
Offline Off
@user Cure Pikachu: LUA ERROR 8: function arguments expected near 'do'
10.08.22 07:18:30 pm
Up
Gaios
Security Supporter
Offline Off
user khaled1968 has written:
@user Cure Pikachu: LUA ERROR 8: function arguments expected near 'do'

Change
for line in file:lines do
to
for line in file:lines() do
10.08.22 09:11:29 pm
Up
khaled1968
User
Offline Off
@user Gaios: It worked! Thanks
To the start Previous 1 Next To the start