Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Does :GetService("Players") print current players or players gotten from when indexed?

local players = game:GetService("Players") 
local children = players:GetChildren()

wait(15) 

for i = 1, #children do 
    print(children[i])
end

If a player leaves between the wait(15), will they still be printed as a child? Also, would "players" refer to "game.Players"?

1 answer

✓ Accepted answer

Duplicate of question.

I will answer this instead.

If a player leaves between the wait(15), will they still be printed as a child?

Yes, but this can cause issues.

The player has left but you still have a reference to them in the table. Improperly handling this kind of stuff can result in a memory leak.

In the for loop you should be checking if the parent of the player is not nil. If it is, remove them from the table.

local Players = game:GetService("Players");
local children = Players:GetPlayers();

wait(15);

for i = 1, #children do
	local client = children[i];
	print(client and client.Parent and client.Name .. " is here" or "Removing client cuz left: " .. table.remove(children, i).Name);
end

Btw use :GetPlayers() over :GetChildren().

This discussion is preserved as it was. New answers, comments, and votes are closed.