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

In pairs loop to get all players doenst work?

Asked by 4 years ago

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?

0
I have updated my post to answer your question :) i_Zesty 8 — 4y

2 answers

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago

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

0
Yeah of course stupid me, thank you luxen1711 9 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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
0
Yeah, thank you but how do i add all the players in the game into a in pairs loop? luxen1711 9 — 4y
0
So i just tested, it works when i do it inside of a LocalScript but not a Script, but does that matter? luxen1711 9 — 4y
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. i_Zesty 8 — 4y

Answer this question