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

Is "for player in ipairs(GetPlayers())" the same as "Players.playeradded:connect(function(Player)"?

Is

for _, player in ipairs(game.Players:GetPlayers()) do

and

game.Players.PlayerAdded:connect(function(Player)

the same? If not then tell me the differences.

2 comments

0

No; one is an event connected to a function which fires whenever a player joins your game and the other is a generic for loop which loops through a table which in this case is all the players in the game. The first method will only work once, and after that, even if new players join it won't do anything.

0

Thx! Alot all of ya. Now I know what to use for my real script.

2 answers

✓ Accepted answer

They're definitly not the same.

The for loop will loop through each player and do whatever you tell it to do to those players

The function will fire once when a player joins, with its first argument being the Players DataModel.

The for loop can be used infinite times to get all the players, the function will only trigger once, for the player that joins at that point.

They are not the same thing, PlayerAdded is an event that fires when a player joins, while

for _, player in ipairs(game.Players:GetPlayers()) do
	
end

is a for loop, that goes through every player.

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