Need help with script (mostly working)

Script: https://pastebin.com/SgaU4iyW

I am trying to create a magic bullet I guess you could say, it’s best if I just explain the idea and how it should work. First, the rules of the bullet:

  • will damage each target one time
  • will damage the closest player within 10 meters of the last target

So let’s say I have original target (aka target 1) then I have target 2 who is 5 meters away from target 1 and target 3 who is 10 meters from target 2 (so 15 meters from target 1). How this should work is I shoot target 1, then it will damage target 2 and from target 2 it will damage target 3. But, instead, it will damage target 1, then damage target 2, then “end” because there are no avalible targets. This is because of the way I wrote the conditions for targetting, if you read the script the last condition for the transfer rule is making sure that the closest player to the current target is the event player. Target 3 is not the closest player to the current target (target 2), target 1 is.

What I want is a way to find the closest player to the current target that has not been targted already. I don’t know how to specifiy that exactly though.

Save the players who got targeted in an array. Then remove them from the “all players” array on the fly like that:

rule ongoing each player

condition:
  *event player fires bullet*

action:
- Event Player.targetPlayers = *first target*
- Event Player.currentTarget = *first target*
- While(True)
- Event Player.temp = Filtered Array(Remove From Array(All Living Players(All Teams), Append To Array(Event Player.targetPlayers, Event Player)), Distance Between(Event Player.currentTarget, Current Array Element) <= 10)
- If(Event Player.temp = Empty Array)
- Break
- End
- Event Player.currentTarget = First Of(Sorted Array(Event Player.temp, Distance Between(Event Player.currentTarget, Current Array Element)))
- Modify Player Variable(Event Player, targetPlayers, Appen To Array, Event Player.currentTarget)
- End