Level Up System Help

Hi guys. I’m currently hitting a block right now as to what I should do to make players level up with their left over exp instead of just leveling up once. As you’re about to see, the code works fine. But there’s one issue. Maximum .Changed function limit is being exceeded at one time, which causes only certain amounts of level ups to happen. It’s like a for loop without a wait() added to it. The script becomes exhausted and that’s the message that you receive in the output. This is basically the same message except it doesn’t lag the game and it does work, but it’s restricting the amount of level ups possible. Any ways to fix this?

local Tool = script.Parent
local Exp = Tool.EXP
local MaximumExp = Tool["Max EXP"]
local Level = Tool.Level
local ExpChanged = Exp:GetPropertyChangedSignal("Value")

local function LevelUp()
	Level.Value += 1
	Exp.Value = Exp.Value - MaximumExp.Value
end

ExpChanged:Connect(function()
	print("Exp changed.")
	if Exp.Value >= MaximumExp.Value then
		LevelUp()
	end
end)
3 Likes

To get remaining exp left → your current exp - the maximum required exp to level up = the exp that you got left after leveling up

1 Like

I think the problem is that you Connect this function everytime the Value is changed. And the second time the value is changed it will connect a second time and run twice. 3rd time run 3 times etc. Try disconnecting the function after each time you connect it

local Tool = script.Parent
local Exp = Tool.EXP
local MaximumExp = Tool["Max EXP"]
local Level = Tool.Level
local ExpChanged = Exp:GetPropertyChangedSignal("Value")

local function LevelUp()
	Level.Value += 1
	Exp.Value = Exp.Value - MaximumExp.Value
end

local connection
connection = ExpChanged:Connect(function()
	print("Exp changed.")
	if Exp.Value >= MaximumExp.Value then
		LevelUp()
	end
    connection:Disconnect()
end)
1 Like

This didn’t work, but I’ve fixed it. I just gave you the solution anyway for your time. Thanks for trying to help! :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.