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

How to get players from server side script?

Asked by 4 years ago
Edited 4 years ago

Hi, newbie question. I have this sample of code that I got from the developer website.

Players = game:GetService("Players")
for i, player in pairs(Players:GetPlayers()) do
    print(player.Name)
end

This code works when I paste it in a local script but it doesn't when I paste it in a server side script. I don't get an error, but nothing gets printed. I am wondering why this is, and also what code do I need to use to get all players from a server side script. Thanks

3 answers

Log in to vote
0
Answered by 4 years ago
wait(#game.Players:GetChildren() > 0)
for i,v in pairs(game.Players:GetChildren()) do
    print(v.Name)
end
0
thanks for suggestion. I have tried this and also wait(2) just to see if it was that players weren't added yet, but it still doesn't work VelocyCharlie 18 — 4y
0
"#game.Players:GetChildren() > 0" returns a boolean, which will the code to halt User#23252 26 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

this should work

@pastel_joga's answer was good, but wait(#game.Players:GetChildren() > 0) will cause the game to halt b/c ur passing a boolean to wait() which expects a number

for i,v in pairs(game.Players:GetChildren()) do
    print(v.Name)
end
Log in to vote
0
Answered by 4 years ago

So far, the only thing that has worked is to call this code inside a PlayerAdded event. Didn't know this was the only option?

Answer this question