Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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!

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. fredfishy 833 — 5y

2 answers

Log in to vote
1
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
Thanks! StrategicPlayZ 58 — 5y
Ad
Log in to vote
0
Answered by
BenSBk 781 Moderation Voter
5 years ago

#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).

Answer this question