• Level System - Help needed!
    6 replies, posted
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) [code] 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 [/code] Client side code: [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 [/code] Please no hate, only love - PIXX Update: Full Code [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) [/code]
I wouldn't use file for stuff like that, I would use [URL="https://wiki.garrysmod.com/page/Player/SetPData"]PData [/URL] [code] -- 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 [/code] [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 :D 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 [URL="https://wiki.garrysmod.com/page/Net_Library_Usage"]net library[/URL]
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 [code] --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) [/code] [code] --cl_init.lua net.Receieve("print_my_xp",function(len) print("Your XP is "..net.ReadInt(16)) end) [/code]
If you need some guidance on making a leveling system: [url]https:--facepunch.com/showthread.php?t=1390994[/url] 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
Sorry, you need to Log In to post a reply to this thread.