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

Maximum event re-entrancy depth exceeded for IntValue.Changed?

Asked by 7 years ago
Edited 7 years ago

So I've been working on a leveling up system for my RPG (which has an XP formula like RuneScape). I've managed to make a carry-over system for it and everything, and it works nicely, to an extent. The problem occurs when I add a lot of XP at a time. In RS, when you add something like 27500 XP, it will put you on level 29, and you'll have some XP leftover, but in my case it doesn't happen.

Example

My Code:

player = game.ServerStorage[Player.Name]

function levelUp()
    if player.MeleeEXP.Value >= player.NextMeleeEXP.Value then
        player.LeftOverMeleeEXP.Value = player.MeleeEXP.Value - player.NextMeleeEXP.Value
        player.MeleeLevel.Value = player.MeleeLevel.Value + 1
        player.MeleeEXP.Value = 0 + player.LeftOverMeleeEXP.Value
        player.LeftOverMeleeEXP.Value = 0
        player.NextMeleeEXP.Value = player.NextMeleeEXP.Value + ( player.MeleeLevel.Value + 300 * (math.pow(2, player.MeleeLevel.Value / 7)) ) / 4
    end
end

player.MeleeEXP.Changed:connect(levelUp)

1 answer

Log in to vote
2
Answered by
Link150 1355 Badge of Merit Moderation Voter
7 years ago
Edited 7 years ago

Understanding the error message

"maximum event re-entrancy depth exceeded" means there is a recursion caused by an event callback.

About Recursion

Before I explain what recursion is, I'd like to bring up an old saying that goes: "To understand recursion, one must first understand recursion".

To put it simply, recursion is when a function calls itself, which calls itself, which calls itself, which calls...

You got the point.

Recursion can be a useful tool to programmers when used correctly but can cause infinite loops that are hard to debug and usually end in a stack overflow. A stack overflow is an error that rises up when a program runs out of stack memory space. The stack is where local variables live and also where the memory address of the previous function is kept so we can return to it later. But this is out of the scope of this question.

Here is an example of a recursive function:

-- Factorial of 'n' implemented using recursion.

local function factorial(n)
    if n == 0 then
        return 1
    else
        return factorial(n * factorial(n))
    end
end

Conclusion

Let's go back to your problem. The error message states "Maximum event re-entrancy depth exceeded for IntValue.Changed?". This is a custom error message thrown by Roblox to limit the number of times an event can be recursively triggered. Here, it would seem, according to the message, that the event at fault is the Changed event of an IntValue object. This is because you change the value of your IntValue object from within your Changed event callback, the function connect()ed to the Changed event.

Rewrite your callback so it doesn't trigger itself and everything should be fine.

Hope this helped. Please mark my answer as accepted and optionally upvote if it did, It helps us both. :)

0
A good way to do this is by moving the line(s) of code where you change the amount of experience to an event that listens for the level value being changed. DefaultAxis 72 — 6y
Ad

Answer this question