I have a script that needs to run through all of the players. It's telling me this error though
ServerScriptService.LevelHandler:2: bad argument #1 to 'pairs' (table expected, got Object)
local players = game:GetService("Players") for _, player in pairs(players) do print("works") end
Why does it think that players is an object?
You need to use game:GetService("Players"):GetPlayers()
Fixed script:
local players = game:GetService("Players") -- Or you can use :GetPlayers() here and on pairs(...) dont need to put it. for _, player in pairs(players:GetPlayers()) do print("works") end
This method returns a table of all presently connected Player. It functions the same way GetChildren would except that it only returns Player objects. It functions similarly to Instance:GetChildren when called on Players. When used in conjunction with a for-loop, it is useful for iterating over all players in a game.
:GetChildren()
/:GetPlayers()
transform this to a "table" for catch players.Hope it helped :D
Wiki pages:
Hi repgainer3,
local players = game:GetService("Players"):GetPlayers() -- Makes it a table with all the players. for _, player in pairs(players) do print("works") end
Thanks,
Best regards,
KingLoneCat