State that changes Element Rate but is Stackable

I want to make a state that causes the affected battler to but more vulnerable to Fire element. But I want the state to be stackable up to 3 times, each stack making the battler 25% more vulnerable to Fire.

So if your element rate for Fire is normally 100%, it could get up to 175% with 3 stacks.

I'm trying to do this with Yanfly's Buff and State Core but I can't figure it out. It is sort of similar to Yanfy's Tips & Tricks Damage over Time Stacking.

Any help with this would be great! Thank you!
Well by default the engine calculates element rate as a multiplicative value, so you can't really "add" 25%. You'd be multiplying by 1.25 (assuming you're setting a trait of 125% fire element rate) and doing that 3 times would mean you're increasing the element rate by 1.25*1.25*1.25 = 1.95, so 195% with 3 stacks (not considering other effects such as equipment that could affect the fire rate as well).
Isn't there an option to have one state remove another? If so, you could have the second state remove the first and the third remove the second and increase the percentages accordingly.
@Frogboy yes you actually can do it like that, though it would still be subjective to other factors that modify the element rate.

You'll need some lunatic code to do so. Firstly, prepare four states one after another so their state ids are consecutive (helps out with the code). We'll use the first one as the actual state that controls everything (like a "master" state), so that's where the lunatic code goes. Set the next three states to have traits of 125%, 150% and 175% fire rate respectively.

In the "master" state, use the following lunatic code:

Code:
<Custom Apply Effect>
target._fireStacks = target._fireStacks || 0;

target._fireStacks += 1;

var stackCount = target._fireStacks.clamp(0, 3);

if (stackCount === 1 && !target.isStateAffected(stateId + 1)) {
target.addState(stateId + 1);
}

if (stackCount === 2 && !target.isStateAffected(stateId + 2)) {
target.addState(stateId + 2);
target.removeState(stateId + 1);
}

if (stackCount === 3 && !target.isStateAffected(stateId + 3)) {
target.addState(stateId + 3);
target.removeState(stateId + 1);
target.removeState(stateId + 2);
}

var eleRate = stackCount * 25;

target.setStateCounter(stateId, '+' + eleRate + '%');
</Custom Apply Effect>


<Custom Remove Effect>
target._fireStacks = undefined;
target.removeState(stateId + 1);
target.removeState(stateId + 2);
target.removeState(stateId + 3);
</Custom Remove Effect>

You don't have to do anything special with the other three states. Just add the "master" state to increase the stage of the state. Removing the master state will remove all stages of the state entirely.
Thanks a bunch!

Sorry for my late response... Naveed your code works great! The only thing I had to change was I put the setStateCounter() into each if statement because I have the "master" state invisible so the setStateCounter() needs to go on states "stateId +1", "stateId +2" and "stateId +3",

But thanks a lot for this! I didn't think of this way of doing it.

Original thread: https://forums.rpgmakerweb.com/threads/state-that-changes-element-rate-but-is-stackable.89793/