Left over EXP help

So I am creating a level system including EXP, but I stumbled upon a problem where if you level up, the value goes to 0 and doesn’t keep the remaining EXP, I’ve looked up some devforum topics about it and I couldn’t really comprehend/understand what they were doing, I tried their solutions but it kinda bugged out for me, here’s the little code that levels up the player, if you can help me to let the player keep the remaining EXP I would highly appreciate it.

Code:

		if levelexp.Value >= maxexp.Value then
			
			levelexp.Value = 0
			level.Value = level.Value + 1
			
			maxexp.Value = maxexp.Value + 200
			
		end

When it reached the maxexp.Value, you create a new variable named as LeftOverEXP, which you can get by subtracting levelexp.Value to maxexp.Value, then set their levelexp.Value to 0 and if LeftOverEXP is more than 1, you just add levelexp.Value with the LeftOverEXP.

1 Like

if levelexp.Value >= maxexp.Value then
    levelexp.Value -= maxexp.Value -- subtract the previous goal so you get remaining xp
    level.Value += 1
    maxexp.Value += 200
end
3 Likes

I’ve tried this but it still bugs out for me, that’s the same one I was talking about

Basically the other guy posted a reply about it, it don’t really work for me like that

I believe it is a working method, can you test it with that?

1 Like

I been tested it, it bugs out for some reason, the exp value gives me an error because it overcame the limit and created a spam, and gave me bunch of levels, which the exp was reset again on 0

That sounds like an issue with how you’re incrementing the exp value.

1 Like

It’s just a .Changed function, nothing else touches the levels

	levelexp.Changed:Connect(function()
		
		if levelexp.Value >= maxexp.Value then
			
			levelexp.Value -= maxexp.Value
			level.Value = level.Value + 1
			
			maxexp.Value = maxexp.Value + 200
			
		end
		
	end)

I ran a test and it worked perfectly fine.


Script I used to test:

game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	
	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	
	local xpGoal = Instance.new("IntValue")
	xpGoal.Name = "XPGoal"
	
	level.Parent = leaderstats
	leaderstats.Parent = player
	
	xp.Parent = player
	xpGoal.Parent = player
	
	xp.Changed:Connect(function(value)
		if value < xpGoal.Value then return end
		xp.Value -= xpGoal.Value
		level.Value += 1
		xpGoal.Value += 200
		warn("Remaining XP:", xp.Value)
	end)
	
	task.defer(function()
		while true do
			wait(5)
			xp.Value += 125
		end
	end)
end)
1 Like

I basically found out what it is, I had to add a wait function to changed, but is there any reliable ways without wait?

Edit: matterfact, I got it fixed nowm appreciate y’all help

I haven’t used a wait() in my Changed event connection and it worked perfectly fine.

1 Like

I just added “wait()” to changed n now it works fine, appreciate it.

1 Like