Revision history [back]
2
No.1 Revision
updated 2010-10-14 14:15:16.360077
41
addhook("join","save_join") -- When join load
function save_join(id)
     if (player(id,"usgn")>0) then
          for line in io.lines("sys/lua/saves/"..player(id,"usgn")..".txt") do
               line = line:split()
               local ms_exp = tonumber(line[1])
               local ms_level = tonumber(line[2])
               level[id]=ms_level
               exp[id]=ms_exp
          end
     end
end

You need to check to see if the file actually exists before buffering the file into Lua.

local filename = "sys/lua/saves/%s.txt"
local file = io.open(filename:format(player(id,"usgn"), "r")
local line
if not file then
    line = {1, 0}
else
    line = file:read("*a"):split()
end
level[id] = tonumber(line[1]) or 1 -- If line[1] is not a number, level[id] becomes 1
exp[id] = tonumber(line[2]) or 0 -- Same as above reasoning (prevents errors)

*nix servers handle line buffering differently. While windows denote newlines via /r/n, linux requires only /n. While it's not a big deal in this case, you do need to keep that in mind when dealing with io.lines.

Also, you declared savedie twice (I think that you meant to declare the first function as saveleave):

addhook("leave","save_leave") -- When you leave it saves
function save_die(id)
     if (player(id,"usgn")>0) then
          io.output(io.open("sys/lua/saves/"..player(id,"usgn")..".txt","w+"))
          io.write(exp[id].." "..level[id])
          io.close()
     end
end

addhook("die","save_die") -- When you die it saves
function save_die(id)
     if (player(id,"usgn")>0) then
          io.output(io.open("sys/lua/saves/"..player(id,"usgn")..".txt","w+"))
          io.write(exp[id].." "..level[id])
          io.close()
     end
end
1
asked 2010-10-14 14:11:22.210592
41
addhook("join","save_join") -- When join load function save_join(id) if (player(id,"usgn")>0) then for line in io.lines("sys/lua/saves/"..player(id,"usgn")..".txt") do line = line:split() local ms_exp = tonumber(line[1]) local ms_level = tonumber(line[2]) level[id]=ms_level exp[id]=ms_exp end end end You need to check to see if the file actually exists before buffering the file into Lua. local filename = "sys/lua/saves/%s.txt" local file = io.open(filename:format(player(id,"usgn"), "r") local line if not file then line = {1, 0} else line = file:read("*a"):split() end level[id] = tonumber(line[1]) or 1 -- If line[1] is not a number, level[id] becomes 1 exp[id] = tonumber(line[2]) or 0 -- Same as above reasoning (prevents errors) Also, you declared save_die twice (I think that you meant to declare the first function as save_leave): addhook("leave","save_leave") -- When you leave it saves function save_die(id) if (player(id,"usgn")>0) then io.output(io.open("sys/lua/saves/"..player(id,"usgn")..".txt","w+")) io.write(exp[id].." "..level[id]) io.close() end end addhook("die","save_die") -- When you die it saves function save_die(id) if (player(id,"usgn")>0) then io.output(io.open("sys/lua/saves/"..player(id,"usgn")..".txt","w+")) io.write(exp[id].." "..level[id]) io.close() end end

Made with Django.