Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to get all players in team separated by comma?

Asked by 1 year ago

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

2 answers

Log in to vote
3
Answered by 1 year ago
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
Ad
Log in to vote
1
Answered by
hellmatic 1517 Moderation Voter
1 year ago

You can do this using a loop: ``` local PlayerNames = ""

-- game.Players:GetPlayers() returns a table of all players -- loop through the table to get each player individually for i, player in pairs(game.Players:GetPlayers()) do PlayerNames = PlayerNames .. player.Name .. ", " -- add the player's name end

print(PlayerNames) ```

Answer this question