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

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

Asked by 2 years ago

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
0
This script runs before you as a player even joins the server, so thats why it doesn't print anything Borrahh 265 — 2y

2 answers

Log in to vote
1
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
Edited 2 years ago

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
Ad
Log in to vote
0
Answered by
W0_0SH 65
2 years ago
Edited 2 years ago

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
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) msculenny 42 — 2y

Answer this question