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

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

Asked by
kjljixx 42
3 years ago
Edited 3 years ago

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 `

0
First of all, to print out the players' names, you don't need the service. It can be done by simply using this: Ilnar_2006duh666 0 — 3y
0
for i, v in pairs(game.Players:GetPlayers()) do print(v) end Ilnar_2006duh666 0 — 3y

2 answers

Log in to vote
0
Answered by
mroaan 95
3 years ago

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)
Ad
Log in to vote
0
Answered by
Creacoz 210 Moderation Voter
3 years ago

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.

0
But is there anything different from using the service and just using game.players? kjljixx 42 — 3y
0
You can use game.Players:GetPlayers() Yes Creacoz 210 — 3y
0
game.Workspace; game.Players, etc... All return the services. Unless they have been renamed, then there should be no errors Creacoz 210 — 3y

Answer this question