So I am trying to retrieve all the players in a team to then be used in future programs. While testing I found out that the player is outputted three times instead of once. I have tried multiple variations off loops and have consulted the documentation for ‘:GetPlayers()’. I have consulted with a friend and it may be a replication issue but I am not totally sure.
Code:
wait(5) --Ensure the player is loaded before running the script
local teams = game:GetService("Teams")
local police = teams.Police:GetPlayers()
local nhs = teams.NHS:GetPlayers()
local fire = teams.Fire:GetPlayers()
for i = 1,#police do
print(police[i])
end
for i = 1,#nhs do
print(nhs[i])
end
for i = 1,#fire do
print(fire[i])
end
Output:
I would highly appreciate if anyone could help me with this as I am stuck for ideas.
Just realised the issue, all the teams had the same TeamColor so it was counting them all as one team. It never occurred to me that the colour would make such a difference as I saw it more as a aesthetic.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.Team = game:GetService("Teams").police
for _, player in ipairs(Players:GetPlayers()) do
print(player.Name.." is in Team"..player.Team.Name)
end
end)
Each time a player joins it should print their name and their team’s name.