Still have questions? Join our Discord server and get real time help.
0

# How to get all players in team separated by comma?

I need to get all players in the team. Every name needs to be a string separated by , . Like this.

local players = "player1","player2","player3"

Im having a problem with it. Im kinda new. I think everyone should understand what do i mean.

0
table.concat would be the easiest method theking48989987 2141 — 1y

3
Edited 1 year ago

As @theking48989987 said, table.concat() is the way to go:

local PlayerTable = {}
for _, player in ipairs(game.Players:GetPlayers()) do
if player.Team == game:GetService("Teams")["Team name here"] then
table.insert(PlayerTable, player.Name)
end
end
local PlayerList = table.concat(PlayerTable, ", ")


Just for your information, table.concat() has four parameters, three of which are optional. The first parameter is required and it must be an array. The second parameter is optional and determines the separator string that will be used between each index in the array when the array is concatenated into one string. The above code will concatenate an array of the players in-game with the separator string ", ". So, each player would be displayed in the string as Player1, Player2, ....

EDIT

In order to make it work, I had to change the script. With the previous line of code I posted, you would have gotten an error about attempting to concatenate a userdata. player.Name is a string and can be concatenated.

0
game.Teams.RedTeam:GetPlayers() will return a list of all players on that team, if you want to make the code shorter. PhantomVisual 967 — 1y
0
I'm aware of that, and I don't care. DeceptiveCaster 3098 — 1y
You can do this using a loop:  local PlayerNames = ""
print(PlayerNames)