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

How to get players from server side script?

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

wait(#game.Players:GetChildren() > 0)
for i,v in pairs(game.Players:GetChildren()) do
	print(v.Name)
end

2 comments

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

0

"#game.Players:GetChildren() > 0" returns a boolean, which will the code to halt

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

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?

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