So, when I was messing around with studio, I decided to make a small game. I made a script to return the Players in the "Players" menu in the Explorer.
I used print to check if the script was working, however, when I ran the game, an error popped up in the output that said " Workspace.Script:3: function arguments expected near 'print' ".
In case you were wondering, this was the test script I used:
local returnedPlayers = game.Players:GetPlayers print(returnedPlayers) -- printed the table
Then I tried to use functions to fix the issue:
local returnedPlayers = game.Players:GetPlayers function testPrint() -- the function print(returnedPlayers) -- printed the table end testPrint() -- calls the function
But the error still persisted. (The only difference was "print" was changed into "function".) However, when I first used studio, it worked fine. Can someone explain what changed or what I am doing wrong?
GetPlayers
. It should be :GetPlayers()
. However, since GetPlayers
returns a table and you try printing a table, it will return its ID, not its contents.for loop
.local PlayersTable = game:GetService("Players"):GetPlayers() for i, v in pairs(PlayersTable) do -- Generic for loop print("Index:", i, "Value:", v) end -- You could also use a numeric for loop for i = 1, #PlayersTable do print(PlayersTable[i]) end
Since this is a table, I would recommend using a for loop. This should work ~~~~~~~~~~~~~~~~~
local returnedPlayers = game.Players:GetPlayers() for _,i in pairs(returnedPlayers) do print(i.Name) end
~~~~~~~~~~~~~~~~~