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

Errors with using functions and print?

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?

1 comment

0

You forgot the (). It should be :GetPlayers()

2 answers

✓ Accepted answer

###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

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

2 comments

0

You do not have to use a for loop, but I find they work a bit better for many reasons.

0

They do work better.

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