How can I check and clear a table's contents?

What I’m trying to do is that every time the player changes team, the table which it’s name was stored clears him.

-- Criminal Code
local allies = {script.Parent.Name,"Civilian"}

game.Players.PlayerAdded:Connect(function(player)
    	player:GetPropertyChangedSignal("TeamColor"):Connect(function()
        if player.TeamColor == BrickColor.new("Bright red") then	
			table.insert(allies,player.Name)
        else	
			table.remove(allies)
			table.remove(allies)
		end
    end)
end)

There are 3 teams in the game with the following TeamColors:
Police - “Really blue”
Citizen - “New Yeller”
Criminals - “Bright red”

That’s why I doubled the table.remove function so if the player changes to citizen and from citizen to police and from police to criminal, it deletes its name from the previous table. I know it’s not efficient nor it works so I came here for some help and feedback.

Do you know how these functions actually works @jefelocaso ?

You are not using the Argumeter in the correct order

His usage is correct, table.insert and table.remove act stack push and pop , respectively , if the index argument is not provided:

local foo = {"bar","fizz",};
table.insert( foo , "buzz" );
print(foo[3]); -- "buzz"

local removed = table.remove(foo);
print(removed); -- "buzz"


For the OP’s question, I am not sure of what you are trying to do, so here is a little script I wrote, I guess?

local allies = {};
local color = BrickColor.new("Bright Red");

game.Players.PlayerAdded:Connect(function(player)
    	player:GetPropertyChangedSignal("TeamColor"):Connect(function()
             local _ = player.TeamColor == color and (table.insert( allies, player.Name) or true) or table.remove(allies , table.find(allies, player.Name) );
    end);
end);

OR

local allies = {};
local color = BrickColor.new("Bright Red");

game.Players.PlayerAdded:Connect(function(player)
    	player:GetPropertyChangedSignal("TeamColor"):Connect(function()
             if player.TeamColor == color then
                     allies[player.Name] = true;
                 else 
                     allies[player.Name] = nil;
             end
    end);
end);

Spacing might be off cause I wrote the code here.

2 Likes

What I’m trying to do is so an NPC constantly checks if the player has the TeamColor Bright red to determine if its name has to be deleted from the table or not

local allies = {script.Parent.Name,"Civilian"}

game.Players.PlayerAdded:Connect(function(player)
    player:GetPropertyChangedSignal("TeamColor"):Connect(function()
        if player.TeamColor == BrickColor.new("Bright red") then	
			allies[player.Name] = true
        else	
			allies[player.Name] = false
		end
    end)
end)

Why not try something like this, then you can simply run the check of
if allies[player.Name] then

1 Like

So it inserts the player name to the table? I also don’t understand the use of the code that @alihsaas provided and you quoted for what I’m trying to do

Apologies, I should have not mentioned the first method and should have left it at the send code cause Second is more efficient than the first. So I will explain the second: It basically connects a Changed event to TeamColor and when it fires it check the TeamColor, if TeamColor is equal to one we created in the variable, I also created a variable for it cause it is more efficient then always calling BrickColor.new(), if it is true, we add Player’s Name as a key and set value to true, if you don’t understand this part, i recommend researching “Dictionaries”, else if it false, i set Player’s Name key to nil, basically removing it from the table. Hope this is good enough explanation.

1 Like

Use table.remove but the way the function works is it takes the table and the index position of the value you want to remove so what you should do is loop through the entire list, do a check for the values your looking for if any and call the function like this [table.remove(table, index)].

1 Like