Is there a way to insert players into table without with making it insert them multi times?

I want to make in 1 of my games a table with active players, I need to list players into the table making sure they are alive with the character and full health

During tests and using prints I found out that the script inserting the players multi times
–Ouput–
player1(x2)
player2
player1
player3

and etc

Tried to use debouce filter but it inserts only 1 player and ignores others , tried to find a solution on the forum and found nothing really helpful to this situation.

wait(30)-- time to make sure my 3 players are loaded before the script runs

print("Starting")

local activeplayers = {}

local allplayers = game.Players:GetChildren()


for i = 1 ,#allplayers do
	if allplayers[i] ~= nil then
		local char = allplayers[i].Character
		if char ~= nil then
			local Humanoids = char:FindFirstChild('Humanoid')
			if Humanoids ~= nil and Humanoids.Health > 0 then
				table.insert(activeplayers, allplayers[i])
			end
			--printing the players name to figure out if it's working.
			for _,v in pairs(activeplayers) do
	    		print(v)
			end
		end
	end
end

Have you tried searching the table for the user before adding the user?
Something like

if activeplayers[player] == nil then
    -- ADD PLAYER
end

Instead of using an array, you could use a dictionary with the player instances being keys.
Instead of

table.insert(activeplayers, allplayers[i])

try

activeplayers[allplayers[i]] = true

Even if you ran that code multiple times it would only have that one value because each time overwrites the key.

1 Like

Tried but maybe wrong it’s still happens

when you add them, do you actually set the value to something else than nil for that key?
Such as

activeplayers[player] = true

The code bellow is the whole code for testing , I am new to tables and don’t understand many things with them

It replays true 6 times does it mean it works the way it’s need to ?

I re-wrote the script using the dictionary method.

wait(30)-- time to make sure my 3 players are loaded before the script runs

print("Starting")

local activeplayers = {}

local allplayers = game.Players:GetPlayers()

for _, player in ipairs(allplayers) do
		local char = player.Character
		if char ~= nil then
			local Humanoids = char:FindFirstChild('Humanoid')
			if Humanoids ~= nil and Humanoids.Health > 0 then
				activeplayers[player] = true
			end
			--printing the players name to figure out if it's working.
			for p,_ in pairs(activeplayers) do
	    		print(p)
			end
		end
end

For some reason it keeps inserting multi times the same player into the table
Capture

local PlayersService = game:GetService'Players';
local PlayersTable = PlayersService:GetPlayers(); -- don't use GetChildren for Players
local ActivePlayers = {};

for _, Player in next, PlayersTable do
if not ActivePlayers[Player.UserId] and Player.Character and Player.Character:FindFirstChildOfClass'Humanoid' and Player.Character.Humanoid.Health > 0 then
ActivePlayers[Player.UserId] = true;
end;
end;

table.foreach(ActivePlayers, print)

I think this is what you want.

Yes , thank you very much and others who helped also, I learned a lot about tables!

Is there some other script that is printing player names?

No there was only 1 script, does it works different for you?

Nevermind, I just realized I left the printing loop nested inside the loop that iterates through the player list.

wait(30)-- time to make sure my 3 players are loaded before the script runs

print("Starting")

local activeplayers = {}

local allplayers = game.Players:GetPlayers()

for _, player in ipairs(allplayers) do
		local char = player.Character
		if char ~= nil then
			local Humanoids = char:FindFirstChild('Humanoid')
			if Humanoids ~= nil and Humanoids.Health > 0 then
				activeplayers[player] = true
			end

		end
end
--printing the players name to figure out if it's working.
for p,_ in pairs(activeplayers) do
	print(p)
end
1 Like

Oh yeah I also didn’t realized but thanks it’s really helping