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