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.
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.
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.
game.Teams.RedTeam:GetPlayers() will return a list of all players on that team, if you want to make the code shorter.
I'm aware of that, and I don't care.
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)
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
table.concat would be the easiest method