Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
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"?

Locked by User#24403

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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().

Ad