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

How can I detect if ENTIRE server is in one team?

Asked by 8 years ago

It gets pass the if statement if only one person joins the black team. How can I structure it to get pass the if statement when everybody is in the black team?

for _,v in pairs(game.Players:GetPlayers()) do
        if v.TeamColor ~= BrickColor.new("Crimson") and v.TeamColor == BrickColor.new("Really black")  then
            script.Parent.AllDead.Value = true
        end
end

1 answer

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

You cannot reasonably do this inside the for loop.

You should consider each player, record if they are all on that team, then check after the loop.


Imagine you had players A, B, C. You want to know if they're all on team team.

That might look like:

allOnTeam = true
    and a.TeamColor == team
    and b.TeamColor == team
    and c.TeamColor == team

(I include the true to show how to write this without making the first one special)

It's easy to make this into a for loop:

local allOnTeam = true
for _, player in pairs(game.Players:GetPlayers()) do
    allOnTeam = allOnTeam and player.TeamColor == team
end

if allOnTeam then
    -- all on one team
else
    -- at least one not on team
end

Another way to approach this is to make a function that tells you who is on a team (and maybe who is not), and to look at the sizes of that.

Since you're probably interested in who is on what team at some point, that's a useful function to have.

Ad

Answer this question