The problem with your code below is that it's checking if they have the exact amount of experience needed to level up. For example, if I have 900 xp, and need 1000 to level up, and killing this NPC gives me 100, then I will level up and everything works. But if killing the NPC gives me 180, I won't level up until I hit the next marker exactly.
Also, unrelated, but I recommend making EXP_STOPATMAX into a boolean instead of an integer.
I see what you mean, I tried doing this:
function AddEXP( ply, args )
ply:SetNWInt("Exp",ply:GetNWInt("Exp")+args)
if table.HasValue(levelups,ply:GetNWInt("Exp")) then
local leveln = 1
for k,v in ipairs(levelups) do if v == ply:GetNWInt("Exp") then leveln = k end end
if leveln == table.Count(levelups) then PrintAll(ply:Name().." has reached the maximum level!")
else PrintAll(ply:GetName().." has now reached level "..leveln.."!") end
ply:SetNWInt("NextLevel",levelups[leveln+1])
ply:SetNWInt("CurLevel",levelups[leveln])
ply:SetNWInt("LevelNum",leveln)
ply:SendLua("surface.PlaySound(\"achievements/achievement_earned.mp3\")")
end
end
And got the bug that you mentioned. I'm not entirely sure on how to fix this, Line 6 is what checks right?
I also tested it by calling it with
concommand.Add("exp_setlevel",function(ply,cmd,args)
if ply:IsAdmin() then
AddEXP( ply, tonumber(args[1]) )
end
end)
table.insert(chatcommands,{"setlevel","exp_setlevel"})
It works but my XP goes over the needed amount as you said it would.