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.
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
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
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)