Add the ID of an actor to an array unless the actor is already in that array?

So, a very skilled user in this forum helped me a lot in the past with the Lunatic State Yanfly.

Thanks to him, I learned how to add the ID of an actor in a variable/array.
But after a lot of testing I just realized that the method I'm using keeps adding the ID of the actor if I use the same skill, even if the ID is already in that array.
The method is the next:

Code:
$game_variables[47] = [] if $game_variables[47] == 0
$game_variables[47].push(self.id)

Now, the problem is that use other skills that check if some actors are in that array. But if I used the same state again, it will add the same ID of said actor to that array. This cause a conflict, where if I use a skill that call for that array, it will use the effect the amount of time of the actor ID is there.

I tried changing the code to:

Code:
$game_variables[47] = [] if $game_variables[47] == 0
$game_variables[47].push(self.id) unless $game_variables[47] = self.id

this method kind of... "worked" but at the same time, it return me a new error.
When I call the variable[47] with a text to check what the array return, it returns me the ID of the actor, but without the brackets [ ]
All this time the array was returning me the ID/s of all actors in that array inside brackets. And I think that's what make it work. But with the method "unless" I'm using it change the array to have no brackets.
It's there a way to make that the array only save the id of said actor once and keeps the brackets?

Thanks in advance!
Try this
Code:
$game_variables[47] = [] if $game_variables[47] == 0
$game_variables[47].push(self.id)
$game_variables[47].uniq!
If my memory serves right, this will remove any duplication.
What TheoAllen wrote would be best, but from the way you were trying it you would have to write it like this
$game_variables[47] = [] if $game_variables[47] == 0
$game_variables[47].push(self.id) unless $game_variables[47].include?(self.id)
First of all I think it's IDIOTIC to use a $game_variables under this circumstance. You know it might end up setting the value of the variable No. 47 to 0, meaning you need to check every so often that it doesn't get reset at any given time. In that case it's better to work with new methods in the Game_System class.

Ruby:
class Game_System
  alias :actor_ary_gm_sys_init :initialize
  def initialize
    actor_ary_gm_sys_init
    @actor_ids = []
  end

  def add_actor_id(aid)
    @actor_ids << aid
    @actor_ids = @actor_ids.uniq
  end
  attr_reader :actor_ids
end

I prefer the uniq method because it will ALWAYS return an array while the uniq! method might end up becoming nil if no duplicate ID was found there.

The Script Calls

$game_system.add_actor_id(ActorID)

puts $game_system.actor_ids
msgbox $game_system.actor_ids
Thanks all of you! It work perfect now! I can't be more grateful.

Original thread: https://forums.rpgmakerweb.com/threads/add-the-id-of-an-actor-to-an-array-unless-the-actor-is-already-in-that-array.149356/