Few problems with Level system

This is my code so far (with help from Cushie)

[lua]
–Configuration
CFG_EXPNEEDED = 100 – How much XP is needed * level.
CFG_EXPOnKill = 20 – How much XP they get on killing.

local meta = FindMetaTable(“Player”)

function meta:SaveLevelTXT() – Save the data of the player
file.Write(“darkrp/Level/”… string.gsub(self:SteamID(), “:”, “") …".txt", self:GetNWInt(“CurLevel”))
file.Write(“darkrp/XP/”… string.gsub(self:SteamID(), “:”, "
”) …".txt", self:GetNWInt(“exp”))
end

function GiveEXP( victim, weapon, killer)
if killer:IsPlayer() then
killer:SetNWInt(“exp”, killer:GetNWInt(“exp”) + CFG_EXPOnKill)
if killer:GetNWInt(“exp”) >= CFG_EXPNEEDED * (killer:GetNWInt(“CurLevel”) + 1) then
for k, v in pairs(player.GetAll()) do
if v != killer then
v:ChatPrint("[XP] “… killer:Nick() …” has reached level “… killer:GetNWInt(“CurLevel”) + 1 …” from killing “… victim:Nick() …”.")
end
end
killer:ChatPrint("[XP] You have reached level “… killer:GetNWInt(“CurLevel”) + 1 …” for killing “… victim:Nick() …”.")
killer:SetNWInt(“CurLevel”, killer:GetNWInt(“CurLevel”) + 1)
killer:SetNWInt( “exp”, 0 )
end
killer:SaveLevelTXT() – Save the new data for the player when they get a kill
end
end
hook.Add(“PlayerDeath”, “GiveEXPOnDeath”, GiveEXP)

function EXPOnSpawn( ply ) – When someone spawns, get their data from the files (If they dont exist, create them with values of 0), and set the player’s NW variables to them.

local sidp = string.gsub(ply:SteamID(), “:”, “_”)…".txt"
if !file.Exists(“darkrp/XP/”… sidp) then
file.Write(“darkrp/XP/”… sidp, “0”)
end
if !file.Exists(“darkrp/Level/”… sidp) then
file.Write(“darkrp/Level/”… sidp, “0”)
end

local exp, level
exp = file.Read(“darkrp/XP/”… sidp)
level = file.Read(“darkrp/Level/”… sidp)
ply:SetNWInt(“CurLevel”, level)
ply:SetNWInt(“exp”, exp)
end
hook.Add(“PlayerInitialSpawn”, “SetEXPOnSpawn”, EXPOnSpawn)

function PrintMyEXP( ply )
ply:ChatPrint( “[XP] You have “… ply:GetNWInt(“exp”) …” exp and you are level “… ply:GetNWInt(“CurLevel”) …”.”)
end
concommand.Add(“PrintMyEXP”, PrintMyEXP)
[/lua]

I have this code inside the lua/autorun/server/ folder, however the XP does go up for each player (bugs a bit and sometimes you go over 100XP and stay at your level. Another problem is that it’s not saving your level/xp. When you first join it creates a file with your steam ID with 0 inside. After killing it doesn’t save the file as it should do. Also, the concommand I added prints “Unknown Command: PrintMyEXP” in chat.

Any help appreciated, Thanks.

Why do people insist on storing values in txt or external databases when GMOD has a conveniently placed internal SQL database for both client and server sides?
Instead of file.write try using player:SetPData() and player:GetPData(), so much easier!
Those values can be stored both serverside and clientside and the clientside db is server-specific (a value stored while playing in one server won’t get overwritten by another server)

[lua]
function GiveMoney(ply,ammount)
local money = ply:GetPData(“Money”,0) --Default to 0 instead of “nil”
ply:SetPData(“Money”, money + ammount)
end
[/lua]

Is your file loading entirely without errors? This issue happens often when the function the concommand tries to access didn’t parse completely.

There is no errors as far as I can see. Nor serverside or clientside.

Edit: I changed to SetPData and GetPData but not sure what to do on this bit:

[lua]
local sidp = string.gsub(ply:SteamID(), “:”, “_”)…".txt"
if !file.Exists(“darkrp/XP/”… sidp) then
file.Write(“darkrp/XP/”… sidp, “0”)
end
if !file.Exists(“darkrp/Level/”… sidp) then
file.Write(“darkrp/Level/”… sidp, “0”)
end
[/lua]

This is my full code just now:

[lua]
–Configuration
CFG_EXPNEEDED = 100 – How much XP is needed * level.
CFG_EXPOnKill = 20 – How much XP they get on killing.

local meta = FindMetaTable(“Player”)

function meta:SaveLevelTXT() – Save the data of the player
//file.Write(“darkrp/Level/”… string.gsub(self:SteamID(), “:”, “") …".txt", self:GetNWInt(“CurLevel”))
//file.Write(“darkrp/XP/”… string.gsub(self:SteamID(), “:”, "
”) …".txt", self:GetNWInt(“exp”))
ply:SetPData(“Level”, self:GetNWInt(“CurLevel”))
ply:SetPData(“exp”, self:GetNWInt(“exp”))
end

function GiveEXP( victim, weapon, killer)
if killer:IsPlayer() then
killer:SetNWInt(“exp”, killer:GetNWInt(“exp”) + CFG_EXPOnKill)
if killer:GetNWInt(“exp”) >= CFG_EXPNEEDED * (killer:GetNWInt(“CurLevel”) + 1) then
for k, v in pairs(player.GetAll()) do
if v != killer then
v:ChatPrint("[XP] “… killer:Nick() …” has reached level “… killer:GetNWInt(“CurLevel”) + 1 …” from killing “… victim:Nick() …”.")
end
end
killer:ChatPrint("[XP] You have reached level “… killer:GetNWInt(“CurLevel”) + 1 …” for killing “… victim:Nick() …”.")
killer:SetNWInt(“CurLevel”, killer:GetNWInt(“CurLevel”) + 1)
killer:SetNWInt( “exp”, 0 )
end
killer:SaveLevelTXT() – Save the new data for the player when they get a kill
end
end
hook.Add(“PlayerDeath”, “GiveEXPOnDeath”, GiveEXP)

function EXPOnSpawn( ply ) – When someone spawns, get their data from the files (If they dont exist, create them with values of 0), and set the player’s NW variables to them.

local sidp = string.gsub(ply:SteamID(), “:”, “_”)…".txt"
if !file.Exists(“darkrp/XP/”… sidp) then
file.Write(“darkrp/XP/”… sidp, “0”)
end
if !file.Exists(“darkrp/Level/”… sidp) then
file.Write(“darkrp/Level/”… sidp, “0”)
end

local exp, level
exp = ply:GetPData(“Level”, self:GetNWInt(“CurLevel”))
level = ply:GetPData(“exp”, self:GetNWInt(“exp”))
//exp = file.Read(“darkrp/XP/”… sidp)
//level = file.Read(“darkrp/Level/”… sidp)
ply:SetNWInt(“CurLevel”, level)
ply:SetNWInt(“exp”, exp)
end
hook.Add(“PlayerInitialSpawn”, “SetEXPOnSpawn”, EXPOnSpawn)

function PrintMyEXP( ply )
ply:ChatPrint( “[XP] You have “… ply:GetNWInt(“exp”) …” exp and you are level “… ply:GetNWInt(“CurLevel”) …”.”)
end
concommand.Add(“PrintMyEXP”, PrintMyEXP)
[/lua]

You can skip that segment of code entirely since you are no longer working with text files (and that segment checks if the files exists before trying to read from them)

[lua]
function EXPOnSpawn( ply ) – When someone spawns, get their data from the PData, or default to 0, and set the player’s NW variables to them.

local exp, level
exp = ply:GetPData(“Level”, 1) --Default to level 1, exp 0 if the value doesn’t exist
level = ply:GetPData(“exp”, 0) --This is because new players won’t have experience!
–Set the exp and level as NWInts because they are frequently accessed by the clients:
ply:SetNWInt(“CurLevel”, level)
ply:SetNWInt(“exp”, exp)
end
hook.Add(“PlayerInitialSpawn”, “SetEXPOnSpawn”, EXPOnSpawn)
[/lua]

BEWARE this will set everyone’s exp to 0 once you implement it!
(But it will save it so you can even restart the server without worries)
But I’ll assume your experience system isn’t working anyways since it wasn’t writing any values to the text files ;b

Yeah got it working now, re-written it all and is now fully working. Thanks anyway :slight_smile:

No problem, hope the SetNWInt and GetNWInt helped :slight_smile: