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

how do i make a list of all players in a game?

Asked by 4 years ago

Im trying to make this script print the names of all players in the game please help!

for i, v in pairs(game.Players:GetPlayers()) do
    print(i)---player priority
    print(v)---players name
end
0
also quick comment, printing i, i believe wont do anything since i in this case is the Players service which apparently is renamed Gameplayer365247v2 1055 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago
game:GetService("Players").PlayerAdded:Connect(function()
    for i,v in pairs(game:GetService("Players"):GetPlayers()) do
        print(v.Name)
    end
end)

when someone joins the game you make a loop through all the players and print their names

0
I think putting Player Removed would also matter. Cyroxite 46 — 4y
0
it keeps saying Player is not a valid member of DataModel ffancyaxax12 181 — 4y
0
so the service is renamed, also poke no it doesnt matter Gameplayer365247v2 1055 — 4y
0
script updated so it works no matter what name the service has Gameplayer365247v2 1055 — 4y
0
@Gameplayer365247v2 it actually does. The person asking the question is probably using this code to get an up-to-date list on the players. So adding a function for a player removed event DOES matter. Cyroxite 46 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

Instead of using an in pairs loop, I would print them in a table so it's easier to read. Here

local playerService= game:GetService("Players")
local playerTable= { }

game.Players.PlayerAdded:Connect(function(player)
table.insert(playerTable[player.Name])
end

game.Players.PlayerRemoved:Connect(function(player)
table.remove(playerTable[player.Name])
end

while wait(15) do
print("playerTable")
end
0
Your Script is also giving me the Player is not a member of datamodel ffancyaxax12 181 — 4y
0
completely unnessesary, you dont need to insert any names into a table, just need to loop through the player list Gameplayer365247v2 1055 — 4y
0
:( Cyroxite 46 — 4y
0
I was thinking about ram usage, so using tables would reduce how much ram would be used. Ram usage is actually kinda important when you're making actual games to improve user experience, but whatever. Cyroxite 46 — 4y

Answer this question