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

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

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

1 comment

0

also quick comment, printing i, i believe wont do anything since i in this case is the Players service which apparently is renamed

2 answers

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

5 comments

0

I think putting Player Removed would also matter.

0

it keeps saying Player is not a valid member of DataModel

0

so the service is renamed, also poke no it doesnt matter

0

script updated so it works no matter what name the service has

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.

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

4 comments

0

Your Script is also giving me the Player is not a member of datamodel

0

completely unnessesary, you dont need to insert any names into a table, just need to loop through the player list

0

:(

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.

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