How would I find the last team alive?

The title says it all. How would I find the last team alive?

I have 4 teams (5 if you include the Spectator team)

Basically, if they have a spawn and they die, they respawn, but if they don’t, they get sent into the Spectator team.

So I basically want to see which team has players when the rest don’t, like a win system for the team basically.

I’ve looked for other solutions but they come up with last player alive. Any ideas?

Thanks

Try this code:

local t1 = 0
local t2 = 0
local t3 = 0
local t4 = 0
for _, i in pairs(game.Players:GetChildren()) do
	if i.Team.Name == "Team 1" then
		t1+=0
	end
	if i.Team.Name == "Team 2" then
		t2+=0
	end
	if i.Team.Name == "Team 3" then
		t3+=0
	end
	if i.Team.Name == "Team 4" then
		t4+=0
	end
end

if t1 > 0 and t2 == 0 and t3 == 0 and t4 == 0 then
	--team 1 win stuff
end
if t2 > 0 and t1 == 0 and t3 == 0 and t4 == 0 then
	--team 2 win stuff
end
if t3 > 0 and t2 == 0 and t1 == 0 and t4 == 0 then
	--team 3 win stuff
end
if t4 > 0 and t2 == 0 and t3 == 0 and t1 == 0 then
	--team 4 win stuff
end

Tell me if it works!

lets assume you are using teams service

you could have it event based where:

whenever a player leaves or player.Team changes, for loop through team objects, excluding spectators, have some sort of active teams table where if #players>=1 then table.insert into activeteams table

at the end of iteration if #activeteams==1 then that team wins, if #activeteams==0 tie, else, continue game

sample code

function CheckWinCondition()

local ActiveTeams={}
--for loop through all teams except specators

for index, team in pairs (TeamsFolder) do
if #team:GetPlayers()>=1 then
table.insert(ActiveTeams, team)
end

end

if #ActiveTeams==0 or #ActiveTeams==1 then
return true
else
return false
end

end

--after initializing game loop and teams are set up

loop through all teams except spectators

for index, player in pairs (team1:GetPlayers())

player:GetPropertyChangedSignal("Team"):Once(function()

--we can assume someteam->Spectator so checkwin condition

if CheckWinCondition()==true then
--stop the game
end


end)

end

I’ll have a look at both of your responses. Thanks for replying!

1 Like

Reading through this topic might be useful, if you need more help tell me:
implementing-a-team-win-system

1 Like

With a little bit of changing up, it works! Thank you so much :wink:
Even if it’s not as efficient, it still works.

This is exactly what I was looking for! However, I already have a system now, thanks for your help.

@Juicy_Fruit Thanks for the explanation as well!
This is now solved.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.