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

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

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

✓ Accepted answer

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.

1 comment

0

Thank you!

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

2 comments

0

How would I edit this for individual teams?

0

Use the getPlayers() function with arguments of the team Instance. So if you wanna find players of blue, do getPlayers(game.Teams.Blue)

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