MZ Creating a Skill that...

My goal with Java script, in which I'm not very proficient, is to create a skill that hits a foe(100%) and those around it to take reduced damage(50%). I am using RPG maker MZ and I also have a number of plugins(battle core). However, I have been unsuccessful in creating that skill exactly how I want it.

Reference : Here

I've presumed to use a skill called "Chain Lighting" which fits the desired effect.

References for the code on using splice...
Reference

JavaScript:
<JS Targets>
  const foes = this.subject().opponentsUnit();
  const target = foes.members()[this._targetIndex];
  targets.push(target);
  let members = foes.aliveMembers();
  members.splice(members.indexOf(target), 1);
  let extraTargets = 2;
  while (extraTargets--) {
  const member = members[Math.randomInt(members.length)];
    if (member) {
      targets.push(member);
      members.splice(members.indexOf(member), 1);
    }
  }
</JS Targets>

However, the result... that I get. When I target a foe, and I execute the skill it hits the target twice and then it does the desired effect. I don't want it to hit the same target twice, only once.
What I'm I doing wrong?
Not wrong, but you could make the first line a bit less convoluted by just saying:
Code:
const foes = target.friendsUnit();
It's not in the documentation, but if this is like the Yanfly plugin counterpart, I'm pretty sure there's already a target variable provided. If that gives you an error, go back to what you had.

Your problem is where you define "const target" and then push that into the targets array. See this point from the plugin instructions:
- The 'targets' variable will include the original set of targets determined
by the skill/item's original scale.

Therefore, the actual target you selected is already in the targets array, and you're defining and pushing it in again.
Not wrong, but you could make the first line a bit less convoluted by just saying:
Code:
const foes = target.friendsUnit();
It's not in the documentation, but if this is like the Yanfly plugin counterpart, I'm pretty sure there's already a target variable provided. If that gives you an error, go back to what you had.

Your problem is where you define "const target" and then push that into the targets array. See this point from the plugin instructions:


Therefore, the actual target you selected is already in the targets array, and you're defining and pushing it in again.
Thank you for your time.
As for the first code reference, it definitely didn't do anything(made the code not work). For reference, I'm using Rpg maker MZ Battle Core. (So I had to keep the code the same)

As for second part, you mentioned that the problem is where it's defined const target = foes.members()[this._targetIndex]; what would you have me do instead?
As for second part, you mentioned that the problem is where it's defined const target = foes.members()[this._targetIndex]; what would you have me do instead?
Nothing - don't push it into the array because it already starts in there. Delete the third line of code.
Nothing - don't push it into the array because it already starts in there. Delete the third line of code.
I- I can't believe it was that simple. I appreciate your help tenfold! Consider this resolve.

JavaScript:
<JS Targets>
  const foes = this.subject().opponentsUnit();
  const target = foes.members()[this._targetIndex];
  let members = foes.aliveMembers();
  members.splice(members.indexOf(target), 1);
  let extraTargets = 2;
  while (extraTargets--) {
    const member = members[Math.randomInt(members.length)];
    if (member) {
      targets.push(member);
      members.splice(members.indexOf(member), 1);
    }
  }
</JS Targets>

Original thread: https://forums.rpgmakerweb.com/threads/creating-a-skill-that.157917/