Implementing A Team Win System

Okay so,

I’ve been working on a game similar to the ROBLOX game bedwars. And I’ve added pretty much everything, except, I’m still missing something. It’s winning the match.

I had an idea similar to the last man standing startegy, where there only one player in the game who’s gonna win. And that’s pretty much easy to make. But, my game supports the squads gamemode. And I really don’t know if this is going to work the same as the startegy mentionned here.

Let me explain more for you.
My game has four teams, each team only handles four players. And we have also four beds. I’ve implemented a system where if you break a team’s bed, a boolean value inside that team sets to true, which is IsBedBroken value. Now, I want it so when a player dies from that team, the server checks if the value is true or false, if its false, the player respawns normally. But, if not. They will just end up spectating all the players. Now, let’s say all the teams beds are broken. And team red has eliminated all the players, and THEY are the only players there, this is where my problem comes. I want to give all the remaining players the win.

So, how can I implement a squad win into my game? You may know what I’m meaning if you’ve played that game before.

Some of my scripts:

RoundHandler

game.Players.PlayerAdded:Connect(function(plr)
	if game.ReplicatedStorage.Intermission.MatchStarted.Value == false then
		module.startRound(5, plr)
	end
end)

game.ReplicatedStorage.Intermission:GetPropertyChangedSignal("Value"):Connect(function()
	if game.ReplicatedStorage.Intermission.Value == "0" or game.ReplicatedStorage.Intermission.Value == 0 then
		for i, player in pairs(game.Players:GetPlayers()) do
			game.ReplicatedStorage.Intermission.MatchStarted.Value = true
			wait(.01)
			player:LoadCharacter()
			wait(.1)
			player.PlayerGui.IntermissionUI:Destroy()
			script:Destroy()
		end
	end
end)

Round module:

local roundModule = {}

function roundModule.startRound(length, player)
	for i = length,0,-1 do
		
		player.CharacterAdded:Connect(function(char)
			wait(1)
			char.PrimaryPart.Position = Vector3.new(-144.226, 222.625, 362.446) 
			player.Backpack:ClearAllChildren()
		end)
		
		task.wait(1)
		
		game.ReplicatedStorage.Intermission.Value = tostring(i)
	end
end

return roundModule

Any help is appreciated!

2 Likes

Are you using roblox’s teams for the squads? Maybe you could remove the team when the bed is destroyed and its players die, then, when there is only 1 team left you can get its members and give them the win.

Yes I am.

Hm, yeah, good idea. I think I can do something like this:

while wait(0.01) do
   if #redTeam:GetPlayers == 0 and bedBroken == true then
      team:Destroy()
      break
   end
end

and for the win:

if player.Team == game.Teams.Red then -- lets say the player is on the red team, same applies for all.
   if #game.Teams:FindFirstChild(player.Team):GetPlayers() >= 1 and #game.Teams.Green:GetPlayers() == 0 and #game.Teams.Blue:GetPlayers() == 0 and #game.Teams.Yellow:GetPlayers() == 0 then
            game.ReplicatedStorage.GiveWin:FireAllClients() -- the winners
       end
   end
end
1 Like

This should probably work fine, but, the loop wouldn’t be efficient. You should try something else instead of it.

Like what?

Also, thanks for helping me!!

Do you remove the player from the team when they get eliminated? There is probably an event that gets fired when a player leaves a team.

Also, no problem for the help.

Yes, when the player dies and the bed is broken. They will be placed on the spectator teams. So, I think we don’t have to remove the team…

What should I use instead of loops, though?

1 Like

Im not very familiar to teams, but if the player is parented under a team, you could check for the team children removing.

Let me check this real quick.

1 Like

Ok, so Teams have an event called PlayerRemoved (Team.PlayerRemoved) You could listen for that event, then check for children when it gets fired.

Example:

redTeam.PlayerRemoved:Connect(function()
if #redTeam:GetPlayers == 0 and bedBroken == true then 

team:Destroy()

end
end)

Alrighty, that’s better than using loops. Thanks. I will try to make a win system using this event and I’ll let you know :smile:

1 Like

Okay, what do you think of this server script?

local teams = game:GetService("Teams")

local redTeam = teams:WaitForChild("Red")
local blueTeam = teams:WaitForChild("Blue")
local greenTeam = teams:WaitForChild("Green")
local yellowTeam = teams:WaitForChild("Yellow")

redTeam.PlayerRemoved:Connect(function(player)
	if #redTeam:GetPlayers() ==0 and redTeam.EggBroken.Value == true then
		redTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

blueTeam.PlayerRemoved:Connect(function(player)
	if #blueTeam:GetPlayers() ==0 and blueTeam.EggBroken.Value == true then
		blueTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

yellowTeam.PlayerRemoved:Connect(function(player)
	if #yellowTeam:GetPlayers() ==0 and yellowTeam.EggBroken.Value == true then
		yellowTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

greenTeam.PlayerRemoved:Connect(function(player)
	if #greenTeam:GetPlayers() ==0 and greenTeam.EggBroken.Value == true then
		greenTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)


game.Players.PlayerAdded:Connect(function(player)
	if player.Team == redTeam then -- player is on the red team, same applies for all.
		if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 then
			print("winner is " .. player.Name)
		end
	end
end)

Egg broken is just bed broken.

I made some huge changes, and… it worked!!

local teams = game:GetService("Teams")

local redTeam = teams:WaitForChild("Red")
local blueTeam = teams:WaitForChild("Blue")
local greenTeam = teams:WaitForChild("Green")
local yellowTeam = teams:WaitForChild("Yellow")

redTeam.PlayerRemoved:Connect(function(player)
	if #redTeam:GetPlayers() ==0 and redTeam.EggBroken.Value == true then
		redTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

blueTeam.PlayerRemoved:Connect(function(player)
	if #blueTeam:GetPlayers() ==0 and blueTeam.EggBroken.Value == true then
		blueTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

yellowTeam.PlayerRemoved:Connect(function(player)
	if #yellowTeam:GetPlayers() ==0 and yellowTeam.EggBroken.Value == true then
		yellowTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

greenTeam.PlayerRemoved:Connect(function(player)
	if #greenTeam:GetPlayers() ==0 and greenTeam.EggBroken.Value == true then
		greenTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)


game.Players.PlayerAdded:Connect(function(player)
	game.ReplicatedStorage.Intermission.MatchStarted:GetPropertyChangedSignal("Value"):Connect(function()
		if game.ReplicatedStorage.Intermission.MatchStarted.Value == true then
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end
		
		redTeam.PlayerRemoved:Connect(function()
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end)
		
		greenTeam.PlayerRemoved:Connect(function()
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end)
		
		yellowTeam.PlayerRemoved:Connect(function()
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end)
		
		blueTeam.PlayerRemoved:Connect(function()
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end)
		
		redTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end)
		
		greenTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end)
		
		blueTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end)
		
		yellowTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
			wait(2)
			if player.Team == redTeam then -- player is on the red team, same applies for all.
				if #redTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #redTeam:GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and redTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					print("winner is " .. player.Name)
				end
			end
		end)
	end)
end)
1 Like

Looks pretty good, but I’m not sure if the Player.Added part works, doesn’t a new player need to join for it to happen? Also, if teams are getting deleted, how would it find them.

Already worked! Thanks so much for your help :slight_smile:

1 Like

No problem, I think the script could be made a bit better by using a for loop to loop between teams instead of doing them all yourself, but this one works, so there isn’t a need. You could also make a function for checking the teams, but no need.

If you need more help, feel free to ask (even though I’m a beginner scripter too).

1 Like

Okay, Thanks for the advice.

Oh really! Thanks!!

1 Like

@craftingboy26 , Hello again.

Sorry for disturbing you but, My system doesn’t work with multiple players…

As you can see, I made a LOT of changes into my system, here’s the code:

local teams = game:GetService("Teams")

local redTeam = teams:WaitForChild("Red")
local blueTeam = teams:WaitForChild("Blue")
local greenTeam = teams:WaitForChild("Green")
local yellowTeam = teams:WaitForChild("Yellow")

game.ReplicatedStorage.Events.RemoveFromTeam.OnServerEvent:Connect(function(player)
	game.Teams:FindFirstChild(player.Team.Name):Destroy()
	player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
end)

game.Teams:WaitForChild("Red").PlayerRemoved:Connect(function(player)
	if #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true then
		game.Teams:WaitForChild("Red"):Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

blueTeam.PlayerRemoved:Connect(function(player)
	if #blueTeam:GetPlayers() ==0 and blueTeam.EggBroken.Value == true then
		blueTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

yellowTeam.PlayerRemoved:Connect(function(player)
	if #yellowTeam:GetPlayers() ==0 and yellowTeam.EggBroken.Value == true then
		yellowTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)

greenTeam.PlayerRemoved:Connect(function(player)
	if #greenTeam:GetPlayers() ==0 and greenTeam.EggBroken.Value == true then
		greenTeam:Destroy()
		player.TeamColor = BrickColor.new("Medium stone grey") -- spectators
	end
end)


local plr

game.Players.PlayerAdded:Connect(function(player)
	plr = player
	game.ReplicatedStorage.Intermission.MatchStarted:GetPropertyChangedSignal("Value"):Connect(function()
		if game.ReplicatedStorage.Intermission.MatchStarted.Value == true then
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end
		
		game.Teams:WaitForChild("Red").PlayerRemoved:Connect(function()
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end)
		
		greenTeam.PlayerRemoved:Connect(function()
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end)
		
		yellowTeam.PlayerRemoved:Connect(function()
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end)
		
		blueTeam.PlayerRemoved:Connect(function()
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end)
		
		game.Teams:WaitForChild("Red").EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end)
		
		greenTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end)
		
		blueTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end)
		
		yellowTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
			task.wait(0.01)
			if player.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
				if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == greenTeam then -- player is on the red team, same applies for all.
				if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == blueTeam then -- player is on the red team, same applies for all.
				if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
			if player.Team == yellowTeam then -- player is on the red team, same applies for all.
				if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
					game.ReplicatedStorage.Events.rewardWin:FireClient(player)
				end
			end
		end)
	end)
end)

game.Teams:WaitForChild("Red").PlayerRemoved:Connect(function()
	task.wait(0.01)
	if plr.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
		if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == greenTeam then -- player is on the red team, same applies for all.
		if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == blueTeam then -- player is on the red team, same applies for all.
		if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == yellowTeam then -- player is on the red team, same applies for all.
		if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
end)

greenTeam.PlayerRemoved:Connect(function()
	task.wait(0.01)
	if plr.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
		if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == greenTeam then -- player is on the red team, same applies for all.
		if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == blueTeam then -- player is on the red team, same applies for all.
		if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == yellowTeam then -- player is on the red team, same applies for all.
		if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
end)

yellowTeam.PlayerRemoved:Connect(function()
	task.wait(0.01)
	if plr.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
		if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == greenTeam then -- player is on the red team, same applies for all.
		if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == blueTeam then -- player is on the red team, same applies for all.
		if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == yellowTeam then -- player is on the red team, same applies for all.
		if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
end)

blueTeam.PlayerRemoved:Connect(function()
	task.wait(0.01)
	if plr.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
		if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == greenTeam then -- player is on the red team, same applies for all.
		if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == blueTeam then -- player is on the red team, same applies for all.
		if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == yellowTeam then -- player is on the red team, same applies for all.
		if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
end)

game.Teams:WaitForChild("Red").EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
	task.wait(0.01)
	if plr.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
		if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == greenTeam then -- player is on the red team, same applies for all.
		if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == blueTeam then -- player is on the red team, same applies for all.
		if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == yellowTeam then -- player is on the red team, same applies for all.
		if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
end)

greenTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
	task.wait(0.01)
	if plr.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
		if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == greenTeam then -- player is on the red team, same applies for all.
		if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == blueTeam then -- player is on the red team, same applies for all.
		if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == yellowTeam then -- player is on the red team, same applies for all.
		if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
end)

blueTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
	task.wait(0.01)
	if plr.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
		if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == greenTeam then -- player is on the red team, same applies for all.
		if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == blueTeam then -- player is on the red team, same applies for all.
		if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == yellowTeam then -- player is on the red team, same applies for all.
		if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
end)

yellowTeam.EggBroken:GetPropertyChangedSignal("Value"):Connect(function()
	task.wait(0.01)
	if plr.Team == game.Teams:WaitForChild("Red") then -- player is on the red team, same applies for all.
		if #game.Teams:WaitForChild("Red"):GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == greenTeam then -- player is on the red team, same applies for all.
		if #greenTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == blueTeam then -- player is on the red team, same applies for all.
		if #blueTeam:GetPlayers() >= 1 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and #greenTeam:GetPlayers() == 0 and #yellowTeam:GetPlayers() == 0 and yellowTeam.EggBroken.Value == true and game.Teams:WaitForChild("Red").EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
	if plr.Team == yellowTeam then -- player is on the red team, same applies for all.
		if #yellowTeam:GetPlayers() >= 1 and #greenTeam:GetPlayers() == 0 and #blueTeam:GetPlayers() == 0 and #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and game.Teams:WaitForChild("Red").EggBroken.Value == true and blueTeam.EggBroken.Value == true and greenTeam.EggBroken.Value == true then
			game.ReplicatedStorage.Events.rewardWin:FireClient(plr)
		end
	end
end)

I also added a die detector as you can see:

script.Parent:WaitForChild("Humanoid").Died:Connect(function()
	if game.Teams:FindFirstChild(game:GetService("Players"):GetPlayerFromCharacter(script.Parent).Team.Name).EggBroken.Value == false then
		for i, Child in pairs(script.Parent:WaitForChild("Humanoid"):GetChildren()) do
			if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
				local Killer = Child.Value
				if Killer:FindFirstChild "leaderstats" and Killer.leaderstats:FindFirstChild "Kills" then
					game.ReplicatedStorage.DiedRE:FireServer(Killer, Killer.KillEffect.Value)
				end
				return
			end
		end
	else
		game.ReplicatedStorage.Events.RemoveFromTeam:FireServer()
	end
end)

Help please?

Are there any errors? Sorry for the long reply, I was sleeping.

I think the problem is the plr, since it will change every time a new player joins.
Do you need to check if the player is in the team? Maybe you can just get the team’s members and fire the win on them using a loop.

Example:

local WinnerTeam
for i,Team in pairs(game.Teams:GetChildren()) do 
if Team.Name ~= "Spectators" then -- so spectator team doesn't count
if #Team:GetPlayers() > 0 then 
WinnerTeam = Team.Name -- The team that won
end
end
for i,Player in pairs (WinnerTeam:GetPlayers()) do
      Event:FireClient(Player)
end

end
end

This is just an example, it might have errors since I just wrote it in mobile.