Number of player in a team

Hello everyone! So, my game has 3 teams: Contestants, Spectators and Completed. How can I make something in my script work only when there are 2 players in “Completed” team?

For example if I wanted 2 players in the SERVER I would say:

if #Players:GetPlayers() = 2 then

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		Countdown = 0 -- End the timer
	end
end)

end

However, I don’t need 2 players in the SERVER, I need 2 players that are alive, that means, 2 players in the Completed team, before the round begins…

How can i change:

“if #Players:GetPlayers() = 2 then” to " if there are 2 players in the Completed team then" ??

1 Like

Try using the GetPlayers function of a team object.

Just like the Players service, the Team Object can be used to also reference the amount of players as well using the GetPlayers() function as @kndn_v mentioned :stuck_out_tongue:

Just reference the Team you want to search for, then use the # operator to convert it into the number of players in that team:

local CompletedTeam = game.Teams.Completed
local GetPlayers = #CompletedTeam:GetPlayers()

if GetPlayers == 2 then
    --Do your code here
end

One thing to keep in mind though, if you call this function the moment you start the simulation in Studio, no players would be loaded in time cause Server Scripts run the moment the server first starts

1 Like

Thank you so much! XD I will have to use that a lot in my scripts