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

Errors with using functions and print?

Asked by 6 years ago

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?

0
You forgot the (). It should be :GetPlayers() User#19524 175 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You forgot the brackets on 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.

To get the contents (specifically, print them), use a 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
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

~~~~~~~~~~~~~~~~~

0
You do not have to use a for loop, but I find they work a bit better for many reasons. CodedWealth 21 — 6y
0
They do work better. User#19524 175 — 6y

Answer this question