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

How to fix Game:Getservice("Players") not working?

So basically when I try this code, instead of printing out all of the player's names, it prints "Players" a bunch of times. I am pretty sure this is referring to game.Players, since when I try doing #Players, it errors out with "Attempted to find length of instance". Is this supposed to happen/How do I fix this without using alternatives e.g. GetChildren. CODE HERE: ` local players = game:GetService("Players") while true do

print(players)

players = game:GetService("Players")

wait(0.1)

end `

2 comments

0

First of all, to print out the players' names, you don't need the service. It can be done by simply using this:

0

for i, v in pairs(game.Players:GetPlayers()) do print(v) end

2 answers

using while true do on detecting a new player is a no-no instead use a function with an event called playerAdded so when somebody joins the game it will fire the function example of the code:

game.Players.PlayerAdded:Connect(function(player)
	print("a new player has joined he's name is "..player)  
end)

You are only getting the service and not an array of the players, services are a bunch of functions.

game:GetService("Players"):GetPlayers() 

Get players being the function, it will return an array of all players, so if you planned on doing #players then it will return the length of the array.

3 comments

0

But is there anything different from using the service and just using game.players?

0

You can use game.Players:GetPlayers() Yes

0

game.Workspace; game.Players, etc... All return the services. Unless they have been renamed, then there should be no errors

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