How do I Insert All Players Names Into a Table?

I have a winner system script which is supposed to insert the names of all players into a table.

The problem with it is that it will only insert a single player into the table.

I tried looking through the Dev Forum, The Wiki, and throughout the internet and found multiple scripts that used to same method as me to accomplish this, but mine didn’t work for some reason.

local round_players = {}
local maps = require(game.ServerScriptService.Maps)
local x = 0

while true do
	wait(0)
	for i, player in pairs(game.Players:GetPlayers()) do
	for i, v in pairs(maps) do
		if game.Workspace.MapStorage:FindFirstChild(v) then
			local round_players = {}
			table.insert(round_players, player.Character.Name)
			print(table.concat(round_players))
			repeat
				wait(1)
				x =  x+1
				print(x)
				player.Character.Humanoid.Died:Connect(function()
				print(player.Name .. " has died!")
				table.remove(round_players, (round_players[player]))
				end)
until x == 45
x = 0

				msg = ""
				for i, playerss in pairs(round_players) do
				if player and player.Parent then
				if (round_players[1]) and (round_players[2]) and (round_players[3]) and (round_players[4]) and (round_players[5]) and (round_players[6]) and (round_players[7]) and (round_players[8]) and (round_players[9]) and (round_players[10]) then
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])..(round_players[3])..(round_players[4])..(round_players[5])..(round_players[6])..(round_players[7])..(round_players[8])..(round_players[9])..(round_players[10])
				elseif (round_players[1]) and (round_players[2]) and (round_players[3]) and (round_players[4]) and (round_players[5]) and (round_players[6]) and (round_players[7]) and (round_players[8]) and (round_players[9]) then	
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])..(round_players[3])..(round_players[4])..(round_players[5])..(round_players[6])..(round_players[7])..(round_players[8])..(round_players[9])
				elseif (round_players[1]) and (round_players[2]) and (round_players[3]) and (round_players[4]) and (round_players[5]) and (round_players[6]) and (round_players[7]) and (round_players[8]) then	
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])..(round_players[3])..(round_players[4])..(round_players[5])..(round_players[6])..(round_players[7])..(round_players[8])
				elseif (round_players[1]) and (round_players[2]) and (round_players[3]) and (round_players[4]) and (round_players[5]) and (round_players[6]) and (round_players[7]) then	
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])..(round_players[3])..(round_players[4])..(round_players[5])..(round_players[6])..(round_players[7])
				elseif (round_players[1]) and (round_players[2]) and (round_players[3]) and (round_players[4]) and (round_players[5]) and (round_players[6]) then
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])..(round_players[3])..(round_players[4])..(round_players[5])..(round_players[6])
				elseif (round_players[1]) and (round_players[2]) and (round_players[3]) and (round_players[4]) and (round_players[5]) then
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])..(round_players[3])..(round_players[4])..(round_players[5])
				elseif (round_players[1]) and (round_players[2]) and (round_players[3]) and (round_players[4]) then
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])..(round_players[3])..(round_players[4])
				elseif (round_players[1]) and (round_players[2]) and (round_players[3]) then
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])..(round_players[3])
				elseif (round_players[1]) and (round_players[2]) then
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])..(round_players[2])
				elseif (round_players[1]) then
					player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..(round_players[1])
				end
			end
		end
	end
end
end
end

By the way, most of the bottom is completely irrelevant to my problem.If it helps, here’s a photo with the important lines circled. Any help would be appreciated.

Thanks for reading! :slight_smile:

2 Likes

I’m overwriting so it resets every round of the game. Every rounds before it adds the current players, it is erasing the data from the last round.

The order of your loops does not make sense. The first loop nested in the while loop iterates over each player, but it takes 45 seconds before it reaches the next player, and you create 45 event connections to Humanoid.Died for each player for each iteration of the while loop. I don’t think any of your code does what you want it to do, not just the code you’re talking about.

To answer your question, though, make a table and then iterate over all of the players and put their names in the table. Or make a table of players and then replace each element with its name.

local names = game.Players:GetPlayers()
for i = 1, #names do names[i] = names[i].Name end
print("Players: "..table.concat(names, ", "))
6 Likes

You should call game.Players:GetPlayers() inside of the maps for loop and handle the round_players table as a local variable in that for loop’s scope. Also, I think it is best practice in terms of convention and code organization to not declare round_players at the top as a local variable and just use it within… a function. You should also eliminate the thrice-nested loop logic because it it hard to read and potentially performance depriving. Maybe define a function that handles maps, one that handles players, and one that handles rounds. These may cooperate and return values that can be utilized in each others’ calls, but it would make your code easier to read and understand. Good luck and I hope your code ends up working! :octopus:

1 Like