Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I remove IntValue.Changed Recursion?

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)

1 comment

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.

1 answer

✓ Accepted answer

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.

4 comments

0

Seems to have worked perfectly. Thanks for the help!

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.

0

In short if the Exp changed fired first and waits it would add mulitple levels onto the players level.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.