XP/Level system glitch (metatables)


local lvl = LocalPlayer():GetLevel()
	local reqxp = 1000 * lvl * 2
	local xp = LocalPlayer():GetXP()
	
	local lvl = LocalPlayer():GetLevel()
	local reqxp = 1000 * lvl * 2
	local xp = LocalPlayer():GetXP()
	
	if xp > reqxp or xp == reqxp then -- had to add a comment to remove keyword operator
		LocalPlayer():AddLevel(1)
		LocalPlayer():SetXP(0)
	end
	
	draw.RoundedBox( 0, ScrW() / 2 - 151, 12, 282, 27, Color(255,255,255) )
	draw.RoundedBox( 0, ScrW() / 2 - 150, 12.5, 280, 25, Color(0,0,0) ) 
	draw.RoundedBox( 0, ScrW() / 2 - 150, 12.5, math.Clamp( xp / reqxp, 0, 1 ) * 280, 12.5, Color(118,158,255) )
	draw.RoundedBox( 0, ScrW() / 2 - 150, 25, math.Clamp( xp / reqxp, 0, 1 ) * 280, 12.5, Color(64,120,255) )
	draw.SimpleText( "Level " .. lvl .. " (" .. xp .. "/" .. reqxp .. ")", "Default", ScrW() / 2, 25, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )

All metatables are set up and save on exit, aren’t nil bla bla bla. Now whenever a player kills someone they get 100 xp. So say they are level 1 they get to 2000 xp like they should for level 2. When they do get to 2k xp it says “Level 2 (0/4000)” just like it should. HOWEVER, a few seconds later it says “Level 1 (0/2000)” and it swaps between them constantly, sometimes resetting to a seemingly random number between 0 and 2000, despite the actual XP amount being 2000.

wat

I wouldn’t recommending doing any of the xp handling clientside, and especially not in what I assume is HUDPaint

when you call :AddLevel and SetXP, you aren’t changing reqxp or xp

[editline]a[/editline]

also, you make a lot of threads, you know there’s a thread small problems like this, right?

I’m setting the XP and level, shouldn’t the variables at the top change with it? How would I do that stuff properly (changing reqxp/xp)

[editline]still annoyed you have to put text here[/editline]
[lua]if xp > reqxp or xp == reqxp then
LocalPlayer():AddLevel(1)
lvl = LocalPlayer():GetLevel()
LocalPlayer():SetXP(0)
reqxp = 1000 * lvl * 2
xp = LocalPlayer():GetXP()
end[/lua]
That should work, right?


local lvl = LocalPlayer():GetLevel()
local reqxp = 2000 * lvl
local xp = LocalPlayer():GetXP()

if xp >= reqxp then -- had to add a comment to remove keyword operator
	LocalPlayer():AddLevel(1)
	LocalPlayer():SetXP(0)

	lvl = lvl + 1
	reqxp = 2000 * lvl
	xp = 0
end

draw.RoundedBox( 0, ScrW() / 2 - 151, 12, 282, 27, Color(255,255,255) )
draw.RoundedBox( 0, ScrW() / 2 - 150, 12.5, 280, 25, Color(0,0,0) ) 
draw.RoundedBox( 0, ScrW() / 2 - 150, 12.5, math.Clamp( xp / reqxp, 0, 1 ) * 280, 12.5, Color(118,158,255) )
draw.RoundedBox( 0, ScrW() / 2 - 150, 25, math.Clamp( xp / reqxp, 0, 1 ) * 280, 12.5, Color(64,120,255) )
draw.SimpleText( "Level " .. lvl .. " (" .. xp .. "/" .. reqxp .. ")", "Default", ScrW() / 2, 25, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )

[editline]a[/editline]

but again, you really should be doing this serverside

I would like to know how I’d do this serverside, yet have the variables update clientside… But, even using your code, it… Still happens…

Maybe this would help explain it better?

are you changing the level anywhere else?

how I would do it serverside is keep a single variable for each player for xp (so level one and a half would be 4000 xp), and just network that whenever it changes

This is the only other place I’m changing XP for now (serverside):
[lua]if ( attacker:IsValid() && attacker:IsPlayer() ) then

	if ( attacker == ply ) then
		attacker:AddDeaths( 1 )
	else
		attacker:AddFrags( 1 )
		attacker:AddXP(100)
	end

end[/lua]