MV Help with a state that grants semi random debuffing states under certain conditions

Hello!
And sorry about the somewhat confusion language. Basically I have a state that is always on the player and whenever the play takes direct HP damage there is a chance of gaining a variety of debuffing statea.
The reasoning for this by the way is Direct HP damage is kinda rare actually since my game makes heavy use of barriers and the player character's barrier refreshes itself in between any fights.

I had gotten one version of the state to work just fine actually, here's the code for that

JavaScript:
<Custom React Effect>
// Check if the effect dealt HP damage
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1) {
var yellowStates = [122, 123, 118];
var x = (Math.random() * 100);
var y = (target.luk - 10);
if (x > y) {
// Make an empty pool of states
  var currentStates = [];
  // Loop through each of the yellow states
  for (var i = 0; i < yellowStates.length; ++i) {
    // Get the currently looped state
    var bonusState = yellowStates[i];
    // Check if the target is affected by that state
    if (!target.isStateAffected(bonusState)) {
      // If it is, add it to the pool of states
      currentStates.push(bonusState);
    }
}
  // Check if the pool of states isn't empty
  if (currentStates.length > 0) {
    // Get a random state from that pool
    var addedState = currentStates[Math.floor(Math.random() * currentStates.length)];
    // Remove the random state
    target.addState(addedState);
    }
  } else {
      target.addState(132);
  }
}
</Custom React Effect>

Basically it checks to see if the PC has any barrier left, and if the last attack that hit them actually did any damage.
If so then it rolls against the PC's Luck stat to determine if they shrug off the injury or get one of the debuffing states.
It works as intended.

But then I tried to make a more completed version, as I meant for there to be tiers to the injuries "yellow" "red" and "black" with red states being more dangerous than yellows and black states being even more detrimental than reds.
Essentially the difference between flesh wounds, open injuries, and a mortal wound.
But when I tried to simply add in a second round of it rolling but this time for the "red" tier states and the whole thing broke. It doesn't even roll for the yellows anymore. Here's the code for that one

JavaScript:
<Custom React Effect>
// Check if the effect dealt HP damage
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1) {
var yellowStates = [115, 117, 122, 123];
var x = (Math.random() * 100);
var y = (target.luk - 10);
if (x > y) {
// Make an empty pool of states
  var currentStates = [];
  // Loop through each of the yellow states
  for (var i = 0; i < yellowStates.length; ++i) {
    // Get the currently looped state
    var bonusState = yellowStates[i];
    // Check if the target is affected by that state
    if (!target.isStateAffected(bonusState)) {
      // If it is, add it to the pool of states
      currentStates.push(bonusState);
    }
}
  // Check if the pool of states isn't empty
  if (currentStates.length > 0) {
    // Get a random state from that pool
    var addedState = currentStates[Math.floor(Math.random() * currentStates.length)];
    // Remove the random state
    target.addState(addedState);
    }
  } else {
      target.addState(132);
  }
}
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1 && target.isStateAffected(115) || target.isStateAffected(117) || target.isStateAffected(122) || target.isStateAffected(123) {
var redStates = [113, 114, 116, 118, 124];
var x = (Math.random() * 100);
var y = (target.luk - 10);
if (x > y) {
// Make an empty pool of states
  var currentStates = [];
  // Loop through each of the red states
  for (var i = 0; i < redStates.length; ++i) {
    // Get the currently looped state
    var bonusState = redStates[i];
    // Check if the target is affected by that state
    if (!target.isStateAffected(bonusState)) {
      // If it is, add it to the pool of states
      currentStates.push(bonusState);
    }
}
  // Check if the pool of states isn't empty
  if (currentStates.length > 0) {
    // Get a random state from that pool
    var addedState = currentStates[Math.floor(Math.random() * currentStates.length)];
    // Remove the random state
    target.addState(addedState);
    }
  } else {
      target.addState(132);
  }
}
</Custom React Effect>


The extra bit of code in the condition was to try and make it only apply if the target already has at least 1 of the yellow tier effects going on before rolling.

And as for the Plugins I've got going on, nearly Yanfly's whole suite. The one's that are particularly relevant off of the top of my head are the absorption barrier plugin, Auto Passives States, and Buffs & States core.

So yea, if anyone can help with this multi layered "injury" debuff state that'd be really appreciated!
Well, I can see at least one error in your code. You ignore operator precedence in the if-condition for your red states logic.(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
Similarly how in math 3 + 10 * 2 = 23 because it is implicitly the same as 3 + (10 * 2) there is an order to && and || operators. Your condition
JavaScript:
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1 && target.isStateAffected(115) || target.isStateAffected(117) || target.isStateAffected(122) || target.isStateAffected(123))
is implicitly the same as
JavaScript:
if ((this.isHpEffect() && value > 0 && target.barrierPoints() < 1 && target.isStateAffected(115)) || target.isStateAffected(117) || target.isStateAffected(122) || target.isStateAffected(123))
But what you actually want is
JavaScript:
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1 && (target.isStateAffected(115) || target.isStateAffected(117) || target.isStateAffected(122) || target.isStateAffected(123)))

Also while typing this out I noticed that the closing round bracket after this if condition is missing in your code.
Well, I can see at least one error in your code. You ignore operator precedence in the if-condition for your red states logic.(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
Similarly how in math 3 + 10 * 2 = 23 because it is implicitly the same as 3 + (10 * 2) there is an order to && and || operators. Your condition
JavaScript:
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1 && target.isStateAffected(115) || target.isStateAffected(117) || target.isStateAffected(122) || target.isStateAffected(123))
is implicitly the same as
JavaScript:
if ((this.isHpEffect() && value > 0 && target.barrierPoints() < 1 && target.isStateAffected(115)) || target.isStateAffected(117) || target.isStateAffected(122) || target.isStateAffected(123))
But what you actually want is
JavaScript:
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1 && (target.isStateAffected(115) || target.isStateAffected(117) || target.isStateAffected(122) || target.isStateAffected(123)))

Also while typing this out I noticed that the closing round bracket after this if condition is missing in your code.

Wow, thanks! That allowed there to be as many debuff checks as I'd like now. Also something to keep in mind for the future about operator precedence.
One more issue however is that currently it now can add multiple debuffs in one go (one from each category).
How can I make it so it can only add 1 debuff state at maximum no matter if the user has the red states "unlocked" or how bad the luk roll is.

Here's the current code with your revision, that works as mentioned:


JavaScript:
<Custom React Effect>
// Check if the effect dealt HP damage
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1) {
var yellowStates = [115, 117, 122, 123];
var x = (Math.random() * 100);
var y = (target.luk - 10);
if (x > y) {
// Make an empty pool of states
  var currentStates = [];
  // Loop through each of the yellow states
  for (var i = 0; i < yellowStates.length; ++i) {
    // Get the currently looped state
    var bonusState = yellowStates[i];
    // Check if the target is affected by that state
    if (!target.isStateAffected(bonusState)) {
      // If it is, add it to the pool of states
      currentStates.push(bonusState);
    }
}
  // Check if the pool of states isn't empty
  if (currentStates.length > 0) {
    // Get a random state from that pool
    var addedState = currentStates[Math.floor(Math.random() * currentStates.length)];
    // Remove the random state
    target.addState(addedState);
    }
  } else {
      target.addState(132);
  }
}
if (this.isHpEffect() && value > 0 && target.barrierPoints() < 1 && (target.isStateAffected(115) || target.isStateAffected(117) || target.isStateAffected(122) || target.isStateAffected(123)))
 {
var redStates = [113, 114, 116, 118, 124];
var x = (Math.random() * 100);
var y = (target.luk - 10);
if (x > y) {
// Make an empty pool of states
  var currentStates = [];
  // Loop through each of the red states
  for (var i = 0; i < redStates.length; ++i) {
    // Get the currently looped state
    var bonusState = redStates[i];
    // Check if the target is affected by that state
    if (!target.isStateAffected(bonusState)) {
      // If it is, add it to the pool of states
      currentStates.push(bonusState);
    }
}
  // Check if the pool of states isn't empty
  if (currentStates.length > 0) {
    // Get a random state from that pool
    var addedState = currentStates[Math.floor(Math.random() * currentStates.length)];
    // Remove the random state
    target.addState(addedState);
    }
  } else {
      target.addState(132);
  }
}
</Custom React Effect>
Currently on a high concept level you do this:
Code:
If conditions for injury
  apply minor injury

If conditions for injury and has minor injury
  apply major injury
This means that if the conditions for an injury are true, first a minor injury will be applied. Then it will be checked whether the conditions for an injury are true (they of course still are) and if you have a minor injury (now you have). Since both conditions are true, a major injury gets applied.

Instead try something like this:
Code:
If conditions for injury and has minor injury
  apply major injury
Else
  If conditions for injury
    apply minor injury
Of course the same concept will also apply with your third injury layer.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-a-state-that-grants-semi-random-debuffing-states-under-certain-conditions.143179/