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

In pairs loop to get all players doenst work?

So I have this loop

player = game:GetService("Players")

for i, v in pairs(player:GetPlayers()) do print(v.." has joined the game") end

But when i play the game nothings prints out what could be the error?

1 comment

0

I have updated my post to answer your question :)

2 answers

✓ Accepted answer

it is working, but because the server runs before any player is on the game, the loop recives a empty table, try adding a wait() before the loop, so your player can load and connect to the server

1 comment

0

Yeah of course stupid me, thank you

I'm not sure how to explain thats wrong, but here is a better solution to it (place this in a normal script in workspace or serverscriptservice)

game:GetService("Players").PlayerAdded:Connect(function(player)
	print(player.Name.." has joined the game.")
end)

and if you want to print when somebody leaves

game:GetService("Players").PlayerRemoving:Connect(function(player)
	print(player.Name.." has left the game.")
end)

"Yeah, thank you but how do i add all the players in the game into a in pairs loop?"

for i, v in pairs(game:GetService("Players"):GetChildren()) do
	print("Player name: "..v.Name)
end

3 comments

0

Yeah, thank you but how do i add all the players in the game into a in pairs loop?

0

So i just tested, it works when i do it inside of a LocalScript but not a Script, but does that matter?

0

It shouldn't really matter, but they should both work in either a LocalScript and a Script, maybe the Script is running just after you join, making it not detect you entering.

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