Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I remove IntValue.Changed Recursion?

Asked by
Rhidlor 42
5 years ago

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)
0
Both changed events set the Value causing the event to run again and again. Remove the level changed event and only change the level in the Exp changed event. User#5423 17 — 5y

1 answer

Log in to vote
-1
Answered by 5 years ago

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.

0
Seems to have worked perfectly. Thanks for the help! Rhidlor 42 — 5y
1
This is not how you fix this code at all. Adding wait() to this causes the function to yield for a small amount of time which will give incorrect results from when it was first fired. User#5423 17 — 5y
0
In short if the Exp changed fired first and waits it would add mulitple levels onto the players level. User#5423 17 — 5y
0
Also, for ValueBase objects, the `Changed` event only fires when their `Value` property changes, oddly enough, so `:GetPropertyChangedSignal()` isn't required here -- although you are right that you SHOULD prefer it to the `Changed` event, when using other objects. Link150 1355 — 5y
Ad

Answer this question