Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[Solved]How to get the number of players in a team or teams?

I am trying to make a battle game with two teams. One is red team and other is blue team. I want that when both teams have equal players, a bool value will be changed. This is the script I have made up till now. It doesn't work and also doesn't show any error in the output.

What is the problem in the script? I have tried a lot but cannot make it work:

local bluechildren = game.Teams.Blue:GetChildren()
local redchildren = game.Teams.Red:GetChildren()
local round = game.ReplicatedStorage.RoundInProgress.RoundInProgress

game.Teams.Red.PlayerAdded:Connect(function()
	if redchildren == bluechildren then
		if round.Value == false then
			round.Value = true
		end
	end
end)

Thanks!

1 comment

0

GetPlayers, not GetChildren for game.Teams. Also, you need to do #redChildren == #blueChildren or you're comparing the values in the list for equality rather than just the number of values in the list.

2 answers

✓ Accepted answer

The # operator returns the length of something, such as a table or a string. You can use this to get the number of items in conjunction with :GetPlayers(). You also want to put bluechildren and redchildren inside the function, as putting it before will make the value static.


local round = game.ReplicatedStorage.RoundInProgress.RoundInProgress

game.Teams.Red.PlayerAdded:Connect(function()

	local bluechildren = game.Teams.Blue:GetPlayers()
	local redchildren = game.Teams.Red:GetPlayers()
    if #redchildren == #bluechildren then
        if round.Value == false then
            round.Value = true
        end
    end
end)

1 comment

0

Thanks!

#Team:GetPlayers() will get the number of Players of a Team.

You should also be using ServiceProvider:GetService() instead of indexing the DataModel to get all services (except optionally Workspace).

This discussion is preserved as it was. New answers, comments, and votes are closed.