How can i print every players name that are in a table

So I have a table with players inside and i want to print every player in a text like this:
“player1, player2, player3” and so on according to how many players that are in the table, but when i do it it only prints one player:

for i, v in pairs(plrs) do
	Status.Value = "The winners are: "..v.Name
end
1 Like

You could use table.concat
See here:

3 Likes

So i tried it and its says “invalid” because its an Instance, so is there a way for converting instance to strings?

1 Like

you need to get the players with :GetPlayers() to get a table filled with the players

1 Like

What you do there is change the value #plrs times while not waiting, meaning that what you will see is "The winners are: [last player in table]" instead the silly method is to cache the string, build it and then set it:

local val = "The winners are: "
for i, v in pairs(plrs) do 
	--imagine this line as adding a player and the comma
	val ..= v.Name..", " --val ..= v is equal to val = val..v
end
val = val:sub(1, val:len()-2) --remove the last comma
Status.Value = val

Also as mentioned above you could use table.concat which is the equivalent of the join operation in python(basically what we did above but cooler):

local names = {}
for _, plr in pairs(plrs) do table.insert(names, plr.Name) end
Status.Value = "The winners are: "..table.concat(names, ", ")
3 Likes

I am inserting the player in a table

2 Likes

oh right, sorry; you can use @NyrionDev solution instead

1 Like

np, thanks for you time! Will try out @NyrionDev solution.

1 Like

Thank you! it worked out perfectly and as expected!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.