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

How do I get this script to find the total number of a certain team?

Asked by 9 years ago

So this script is supposed to detect how many people are on a certain team, and react to it. However, it isn't working, and I don't know why.

Can someone please help me? Here's the script:

local greenTeam = {}
    for _,v in pairs(game.Players:GetPlayers()) do
        if v.TeamColor == BrickColor.new("Bright green") then
            table.insert(greenTeam,v)
        end
    end

while wait() do
    if #greenTeam == 0 then
        print('Green Team Ded')
    end
end

2 answers

Log in to vote
0
Answered by
DrJonJ 110
9 years ago

Here's a script that does what you asked and a little neater:

local team = game.Teams;

function getPlayers(teamColor) -- team color is easier than name
    local found = { };
    for _,v in pairs(game.Players:GetPlayers()) do
        if v.TeamColor == teamColor then
            table.insert(found, v)
        end
    end
    return #found
end

repeat -- SEE NOTE AT BOTTOM
    wait() until getPlayers(BrickColor.new('Bright red')) == 0
              or getPlayers(BrickColor.new('Bright yellow')) == 0
              or getPlayers(BrickColor.new('Bright green')) == 0
              or getPlayers(BrickColor.new('Bright blue')) == 0


-- do stuff

NOTE: Using a repeat will STOP once one is detected to be true. You can remove, add, or use and statements if you like.

0
Thank you! CoolJohnnyboy 121 — 9y
Ad
Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your problem is that you're never recalculating the team. So it the table will always return the same results as when you first defined it. To fix this, define the table in the loop.

Here's an example of a more efficient code;

local teams = game.Teams:GetChildren()

function getPlayers(team)
    local t = {}
    for i,v in pairs(game.Players:GetPlayers()) do
        if v.TeamColor == team.TeamColor then
            table.insert(t,v)
        end
    end
    return t
end

while wait() do
    for i = 1,#teams do
        local tab = getPlayers(teams[i])
        if # tab == 0 then
            print(teams[i].Name..' Team Died')
        end
    end
end
0
How would I edit this for individual teams? CoolJohnnyboy 121 — 9y
0
Use the getPlayers() function with arguments of the team Instance. So if you wanna find players of blue, do getPlayers(game.Teams.Blue) Goulstem 8144 — 9y

Answer this question