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

How do i use i, v in pairs with a playerlist?

I simply just want to print the names of the players in the server, though my script doesn't work (it doesnt show any errors though), it is a ServerScript placed in workspace

local getPlayers = game.Players:GetPlayers()

for i, v in pairs(getPlayers) do
	print(i.." = "..v.Name)
end

1 comment

0

This script runs before you as a player even joins the server, so thats why it doesn't print anything

2 answers

✓ Accepted answer

this will ensure the script runs if theres a player

local Players = game:GetService("Players"):GetPlayers()
repeat
	task.wait()
	Players = game:GetService("Players"):GetPlayers()
until Players[1]


for i,v in pairs(Players) do
	print(i,v)
end

The in pairs loop probably executed before a single player even connected, so all we have to do is put a wait(). Something like this.

wait(1)

local getPlayers = game.Players:GetPlayers()

for i, v in pairs(getPlayers) do
    print(i.." = "..v.Name)
end

1 comment

0

No, It's because when you made the variable for "getPlayers", there where no players in the game, so the table is empty. You can try like this: game.Players.PlayerAdded:Connect(function() --loop through players end)

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