So I'm getting the error below because the events are calling each other continuously sometimes, for instance if a player gets a very large amount of exp. How can I retain functionality and fix this error?
Error: Maximum event re-entrancy depth exceeded for IntValue.Changed
game.Players.PlayerAdded:Connect(function(Player) local Stats = Player:WaitForChild("Stats") local Exp = Stats:WaitForChild("Exp") local Level = Stats:WaitForChild("Level") local LastLevel = Level.Value local RequiredExp = math.floor((Level.Value * (Level.Value / 2)) + 10) Level.Changed:Connect(function() if Level.Value > LastLevel then if Exp.Value >= RequiredExp then Exp.Value = Exp.Value - RequiredExp end else Exp.Value = 0 end LastLevel = Level.Value end) Exp.Changed:Connect(function() RequiredExp = math.floor((Level.Value * (Level.Value / 2)) + 10) if Exp.Value >= RequiredExp then Level.Value = Level.Value + 1 end end) end)
I recommend changing .Changed to :GetPropertyChangedSignal("Value"):Connect(function() as well as adding a wait time before running anything in the changed function.
Your code should look like this:
game:GetService("Players").PlayerAdded:Connect(function(Player) local Stats = Player:WaitForChild("Stats") local Exp = Stats:WaitForChild("Exp") local Level = Stats:WaitForChild("Level") local LastLevel = Level.Value local RequiredExp = math.floor((Level.Value * (Level.Value / 2)) + 10) Level:GetPropertyChangedSignal("Value"):Connect(function() wait() if Level.Value > LastLevel then if Exp.Value >= RequiredExp then Exp.Value = Exp.Value - RequiredExp end else Exp.Value = 0 end LastLevel = Level.Value end) Exp:GetPropertyChangedSignal("Value"):Connect(function() wait() RequiredExp = math.floor((Level.Value * (Level.Value / 2)) + 10) if Exp.Value >= RequiredExp then Level.Value = Level.Value + 1 end end) end)
Hope this helped.