How can I make a list of winners in a string?

When the game is over, I want it to list the winners. All of the winners will be on the team called “Playing.”

If there are multiple winners I want it to list like this:
Winners: Player1, Player2, Player3.

If there is one winner I want it to remove the plural:
Winner: Player1.

If there are no winners I want it to say this:
Winners: Nobody.

Please help me do this!

local teams = game:GetService("Teams")
playingPlrs = teams.Playing:GetPlayers()
if #playingPlrs > 0 then
	local playingString = table.concat(playingPlrs, ", ")
	print(playingString.." won!")
else
	print("Everyone lost!")
end

So, this?
image

No, it didn’t work. It’s this line that doesn’t work:

local playingString = table.concat(playingPlrs, ", ")

It does work but for some reason you tried converting the table into a string in your implementation, use the line of code as I provided it.

table.concat() returns a string value.

I know, I need to figure out how to get a list of player names. I will make a post about it.

What you have works, just remove “tostring()”, the variable named “playingPlrs” is already an array of the team’s players.

table.concat() takes two arguments, the first is the table which is being concatenated the second is a string value representing how to separate (delimit) each value in the table being concatenated, the function returns a string value with this string being all of the values of the table concatenated into a single string with each value being separated from one another by the specified delimiter.

Well it doesn’t work because the “playingPlrs” array is a list of player objects and not strings, so I can’t concat those.

This line works because it’s just one item in the list:

if #playingPlrs == 1 then
	status.Value = ("Winner: "..tostring(playingPlrs[1])..".")
else

But it’s this line that doesn’t work:

local playingString = table.concat(playingPlrs, ", ")

I changed it to this, and right now I’m testing if it will work:

local playingString = table.concat(tostring(playingPlrs), ", ")

I’ll give an update if it doesn’t work

Update: It didn’t work, gave an error saying “table expected, got string”

I need to figure out how to make the array into the player’s name instead of the object then

local teams = game:GetService("Teams")
local playingPlrs = teams.Playing:GetPlayers()
local playingPlrsNames = {}

if #playingPlrs > 0 then
	for _, player in ipairs(playingPlrs) do
		table.insert(playingPlrsNames, player.Name)
	end
	local playingString = table.concat(playingPlrsNames, ", ")
	print(playingString.." won!")
else
	print("Everyone lost!")
end
1 Like

For some reason I overlooked the original players array being an array of player objects not player names, the above should grab the names, insert them into a new array and concatenate that instead.