How to make a repeatedly revive state?

I want to make a revive state, like Yanfly's Phoenix ring. But I also want to make it be able to revive the player revive 1 time or 3 times in a battle, but the item won't disappear.

Here's my code:
Code:
<Custom Apply Effect>
    var X = 0;
    var X = X + 1 ;
    if ( X == 3 )
        var i = 417 ;
    if (target.isStateAffected(i)) {
        target.startAnimation(49);
        var hp = 1;
        target.gainHp(hp);
        target.startDamagePopup();
        target.removeState(i);
    }
    if ( X != 3 )
        var i = 417 ;
    if (target.isStateAffected(i)) {
        target.startAnimation(49);
        var hp = 1;
        target.gainHp(hp);
        target.startDamagePopup();
}
</Custom Apply Effect>

But player revive again and again......unlimited.
I'm not super-familiar with Yanfly's State Core, but the fact that you are setting X to 0 in the same block of code that you're incrementing and then checking it sticks out as a likely problem. I'm guessing that because of this, X will always be 1 and therefore the != 3 part will always be run.

Make sure you only initialize X once - I think that Yanfly uses the || "or" notation to do things like this, meaning that replacing "X = 0" with "X = (X || 0)" would cause it to be set equal to 0 the first time the block runs, and set equal to itself all other times. I'm not 100% sure this is the right fix, but I'm fairly confident that your repeated initialization to 0 is why you're getting infinite revives.
I'm not super-familiar with Yanfly's State Core, but the fact that you are setting X to 0 in the same block of code that you're incrementing and then checking it sticks out as a likely problem. I'm guessing that because of this, X will always be 1 and therefore the != 3 part will always be run.

Make sure you only initialize X once - I think that Yanfly uses the || "or" notation to do things like this, meaning that replacing "X = 0" with "X = (X || 0)" would cause it to be set equal to 0 the first time the block runs, and set equal to itself all other times. I'm not 100% sure this is the right fix, but I'm fairly confident that your repeated initialization to 0 is why you're getting infinite revives.

When I saw your reply, suddenly I remember Yanfly's Video......DOT Stacking.
So I fix my code to:

Code:
<Custom Apply Effect>
    target._stackingX = target._stackingX || 0;
    target._stackingX += 1;
    if ( target._stackingX == 3 )
        var i = 417 ;
    if (target.isStateAffected(i)) {
        target.startAnimation(49);
        var hp = 1;
        target.gainHp(hp);
        target.startDamagePopup();
        target.removeState(417);
    }
    if ( target._stackingX != 3 )
        var i = 417 ;
    if (target.isStateAffected(i)) {
        target.startAnimation(49);
        var hp = 1;
        target.gainHp(hp);
        target.startDamagePopup();
}
</Custom Apply Effect>

Thank you!
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-a-repeatedly-revive-state.90869/