Level System - Help needed!

Hello. I am quiet new to Saving And Loading. In fact, never used so much, but I know how to do “file.Read” and “file.Write” but how do I use it with numbers, it will just go to a string and well… “fuck up my system”.

So I am asking you, the community of facepunch, the beautiful, help one.

My code so far:
server side code: (Yes, I know it is bad, does not even work)



function WriteDocument(ply)
if IsValid(ply) then
file.Write(ply:GetName().."_level.ini",level)
file.Write(ply:GetName().."_xp.ini",xp.."/"..xp_max)
file.Write(ply:GetName().."_year.ini",year..yeartag)
else end
end


Client side code:



function LoadDocument(ply)
if IsValid(ply) then
if file.Exists(ply:GetName().."_level.ini") then
print(file.Read(ply:GetName().."_level.ini"))
print(file.Read(ply:GetName().."_xp.ini"))
print(file.Read(ply:GetName().."_year.ini"))
else
print("Something is wrong!")
end
end
end


Please no hate, only love

  • PIXX

Update:
Full Code



local level = 1
local max_level = 100

function levelUp()
xp = 0
level = level + 1
PrintMessage(HUD_PRINTTALK,"LEVEL UP! You are now: "..level)
end

function MyLvl(ply)
PrintMessage( HUD_PRINTTALK, "You are level: "..level..". Year: "..year.." "..yeartag)
end


local ply = FindMetaTable('Player')

function ply:AddXP(amt)
 local xp = self:GetPData("xp") or 0
 xp = xp + amt
 ply:SetPData("xp",xp)
end


function giveXp()
ply:AddXP(25)
end

function xpCountdown()
if countdown != 0 then
countdown = countdown - 1
elseif countdown == 0 then 
giveXp()
end
end

concommand.Add("lvl",function(args)
MyLvl()
end)

concommand.Add("xp",function(ply)
PrintMessage(HUD_PRINTTALK,"XP: "..tostring(xp))
end)

function checkIfXp()
if tostring(xp) != nil then
if tostring(xp) == "50" then
levelUp()
end
end
end


timer.Create("xpcount",1,0,xpCountdown)
timer.Create("checkXp",1,0,checkIfXp)


I wouldn’t use file for stuff like that, I would use PData



-- get the player meta table so we can add some functions to it
local ply = FindMetaTable('Player')

function ply:AddXP(amt)
 -- get the current xp, if it doesn't exist use 0
 local xp = self:GetPData("xp") or 0
 -- add the amt
 xp = xp + amt
 -- save
 ply:SetPData("xp",xp)
end


[editline]4th August 2016[/editline]

Also, you can only really use PData on the server, if you set PData on the server then try retrieving the same value on the client it won’t work, because the client and server are running seperately.

Also, If you want to use file, I think the reason your code doesn’t work would be because you’re writing values on the server and trying to retrieve them on the client.

If you’re doing this locally, i.e. singleplayer, then you shouldn’t have an issue as the client and the server are the same machine, but that won’t work in a real world server / client setting.

Jaooe, thanks man will try :smiley:

Update:
When I try to print XP it returns NIL.

So I do not know how to print it, well, it prints nil, but I need a number!

Are you trying to read the PData on the client?

[editline]4th August 2016[/editline]

To communicate between the client and server you should use the net library

Oh… my bad

Edit:
I have added the thing to the cl_level.lua

But, it still says: XP: Nil

So what do I do?

you can’t use PData on the client, you need to send the PData to the client using the net library, an example would be



--init.lua
...
AddXP code...
...

concommand.Add("print_my_xp",function(ply,cmd,args)
 net.Start("print_my_xp")
  net.WriteInt(ply:GetPData("xp"),16)
 net.Send(ply)
end)




--cl_init.lua
net.Receieve("print_my_xp",function(len)
 print("Your XP is "..net.ReadInt(16))
end)


If you need some guidance on making a leveling system: https:–facepunch.com/showthread.php?t=1390994

Using the example above, you could add a few extra functions:
(All Serverside now)
[lua]
– Save the players xp
function playerMeta:SaveXP()
–Get their xp
local xp = self:GetXP()
–Save it to a file
file.Write(self:SteamID() … “.txt”, xp)
–Visual Feedback
print("Saved " … self:Nick() … “'s XP”)
end

-- Save the players xp
function playerMeta:LoadXP()
	if file.Exists(self:SteamID() .."_xp.txt") then
		--If they have a sav file get their XP and set it for them
		local loadedXP = file.Read(self:SteamID() .. "_xp.txt")
		self:SetXP(loadedXP)
	else
		--If they don't have a save file then set their xp to 0 and then save
		self:SetXP(0)
		self:SaveXP()
	end
	--Visual Feedback
	print("Loaded " .. self:Nick() .. "'s XP'")
end

[/lua]

Then if you want to quickly save all player’s xp then you can do something like:

[lua]
function SaveAllPlayerXP()
–Loop through all players and save their xp
for k,v in pairs( player.GetAll() ) do
v:SaveXP()
end
end
[/lua]

If you got any questions to explain anything, shoot. Anyway good luck!

Edit: Changed // to --. Too used to PHP, appologies