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