Error when trying to use 'length'

I have a skill that would target an enemy and remove all its states (besides Dead, Guard and Immortal). I did not want to go through the motion of selecting all states one by one in the skill screen so I made the skill call a common event.

In the Common event I tried to call the following command:
1696535274711.png


but it does give me the following error in the debug console:
1696535298719.png


I am not at all good at programming and I am just repurposing bits of code I happen to find here and there. I suppose what happens here is that "this" actually targets nothing.

Do you have any idea how to fix it with code? I have a workaround that is a little dumb but does the job if I cannot do it with a simple line of code but still, I prefer to use something more elegant than that.
I suppose what happens here is that "this" actually targets nothing.
You nailed it! "this" refers to the Common Event itself but not to any Actor.

Unfortunately, I cannot find an intuitive way to actually solve your query, but maybe somebody else has more success.
"this" does not refer to the enemy. It's also a bit odd that you're starting at the fourth element of their states (which you should call using the getter function states() ).

You also probably don't want to bypass functionality by going so low level as using eraseState().

I'm presuming your Dead, Guard, and Immortal states are the default IDs, so try:
Code:
$gameTroop.members()[$gameTemp.lastActionData(5)].states().forEach(state => {if (![1, 2, 3].includes(state.id)) $gameTroop.members()[$gameTemp.lastActionData(5)].removeState(state.id)})
Thank you both. In your example ATT_Turan, how do I target the enemy that I want to remove the states of? The skill actually targets enemies. Does lastActionData(5) does that?

Otherwise my workaround currently is to apply a state (specifcally for that skill) and then check which battler has the state and has all its states cleared while the Guard is being reapplied. We might use that specific state to determine which enemy we are trying to clear the states of. And we are not going to be able to have multiple targets anyway, since the skill will remove all states anyway.

Also, what is the difference between eraseState and removeState?
Thank you both. In your example ATT_Turan, how do I target the enemy that I want to remove the states of? The skill actually targets enemies. Does lastActionData(5) does that?
Correct. If you go through the event commands, Control Variable has a Last option with several last things to choose from, including last enemy index. This is just the script call for that.

Otherwise my workaround currently is to apply a state (specifcally for that skill) and then check which battler has the state
That's an old workaround that isn't necessary in MZ with its addition of the last data.

Also, what is the difference between eraseState and removeState?
removeState() performs several other game functions automatically, such as refreshing the battler so it will no longer display the state's icons or overlay. Jumping directly to eraseState() only literally removes the state from the list.
It is safe to say that at this point I do not really get what the code exactly does (more so how it does it).
I tried implementing it in a common event and in the damage formula. Both did not work. For some reason the first time I casted it nothing happened but the second time I got prompted with this:
1696538491881.png
You should not put it into the damage formula, I don't believe the Last variable is properly set at that point.

Have the skill call a common event and paste that into a Script box in that event. If you get errors there, please post that screenshot.

But if you're using VisuStella's Battle Core, this can be way easier by just using the notetags given to you in those instructions.

Just give the skill the notetag:
Code:
<JS Post-Apply>
target.states().forEach(state => {if (![1, 2, 3].includes(state.id)) target.removeState(state.id)});
</JS Post-Apply>
That worked like a charm! Thank you!

Trying to repurpose it by including buffs and debuffs. I tried the following by adjusting your code but it expectedly just errored. Not sure what am I missing though. Most plugins refer to the ATT, SPD, MDF etc. modifiers as "buffs" and "debuffs", so I am not sure what their official title is.
target.buffs().forEach(buff => target.removeBuff(buff.id));
target.debuffs().forEach(debuff => target.removeDebuff(debuff.id));

1696556163008.png


Although those (at least) could be done manually without being too much of a pain since they are a small finite number.
As a tip, if you don't know what you're doing with code, it really doesn't make sense to guess. Code is very specific, the odds of you just happening to type the thing you need are close to zero.

If you would spend time guessing, it makes more sense to leave the question for help someplace then spend that time doing some tutorials. At least that way you'll eventually be able to do some stuff on your own if you want to 😉 (and if you don't care about learning JavaScript, spend the time working on your project instead)

To do what you want, you can put in a notetag target.clearBuffs()
Hey, thanks again.

I did start my project with the intent to learn JavaScript along with it. I did some basic courses and then some less basic ones. I never got far in terms of it benefiting the game though. It seems that despite that I grasped some basic concepts after the courses, that lead me nowhere since I needed to know what's going on in the code base that a game starts with in order to be able to even start contributing. I never managed to make that step up. Finished only one custom plugin and gave up.

However, the base I set for my game in terms of skills, items and the way they work is somewhat ambitious due to the fact that I expected to be able to code it eventually. And that's why I am guessing now, trying to re-purpose code or straight out asking in the forums. Sorry if I am bothering you with so many questions.

Original thread: https://forums.rpgmakerweb.com/threads/error-when-trying-to-use-length.161774/