MZ If a character is in your party, when enemies attack they will lose HP.

Hi everyone !

I'm searching for a script as described in the title.

I explain ;
One of my character is a Ninja and he's using makibishis
(little iron stars ninjas drop on the floor to slow down enemies "irl")
So I would like to reproduce this in RPGmaker MZ ;

when an enemy attacks the party with the ninja in, he will lose -5% HP
exepted if this enemy can fly
;

so I would like to add this malus only on certain kind of enemies.

I installed the VisuStella's plugin (amazing btw) but it's working for states and not for passiv skill who can reduce enemies life.

I also tried to work with conditionnal branch but maybe didn't see the right things .

Anyway here I am now looking for a script or for someone who could help how to do.

(I saw nothing on google btw maybe I didn't search well but I'm not english so maybe there are some subtilities with keywords I may don't know)

Thx for your answers ! Hope it will be helpful for someone internet else too !
I installed the VisuStella's plugin (amazing btw) but it's working for states and not for passiv skill who can reduce enemies life.
I would do this with states.

There are different ways you could do it, putting a passive state on the entire party that triggers when they're the target of an attack (in which case I would also make "flying" a passive state that you put on specific enemies to bypass the code).

Or you could put a turn 0 event into each of your troops that checks for the party member to know this skill and then apply a state to each of the enemies that you decide should have it, and it triggers code to hurt them after they make attacks.

Either way you'll need some JavaScript in there. You could check out Yanfly's Tip and Trick for Thornmail which might be a helpful start.
This should do what you are asking for. (Caveat: it's late here and I haven't thoroughly tested).

JavaScript:
const Mechazard_executeHpDamage = Game_Action.prototype.executeHpDamage;
Game_Action.prototype.executeHpDamage = function(target, value) {
    Mechazard_executeHpDamage.call(this, target, value);
    if (BattleManager._subject.isEnemy() && target.isActor() && $gameParty.hasNinja()) {
        const enemy = BattleManager._subject;
        if (!enemy.isFlying()) {
            const damage = Math.max(1, Math.round(enemy.mhp * 0.05));
            enemy.gainHp(-damage);
            enemy.startDamagePopup();
        }
    }
}
Game_Party.prototype.hasNinja = function() {
    const ninjaClassId = 3; // change 3 to your ninja class ID
    for (const actor of this.battleMembers()) {
        if (actor._classId === ninjaClassId) return true;
    }
}
Game_Enemy.prototype.isFlying = function() {
    return this.enemy().meta.Flying;
}

Add the <Flying> notetag to your flying enemies.

Might need some more details. For example, if you are using the front-view battle system you'd probably want to add a battle log window message to indicate that the effect triggered.
Woaw I wasn't expected for that clean code that fast lmao this is perfect thank you very much !!

Original thread: https://forums.rpgmakerweb.com/threads/if-a-character-is-in-your-party-when-enemies-attack-they-will-lose-hp.147896/