Implementing A Team Win System

Oh hey!

Yes, one of them is when destroying a team, it doesn’t get the destroyed team again. If you don’t understand what I mean, I tried it with two people in one server, and I basically broke all the beds and eliminated the player wuth me, they got sent to the spectators team and their team got destroyed. Instead of giving me the win, it game me another error saying that the red team should be in the teams service.

Oh, maybe you shouldn’t destroy the team since we are checking for players.

Or maybe, instead of checking for a team’s players, you could destroy the team then, use

game.Teams.ChildRemoved:Connect(function()) -- Updates each time a team is destroyed
        local TeamsLeft = 0
        local Winner
       for i, Team in pairs(game.Teams:GetChildren()) do
      if Team:IsA("Team") and Team.Name ~= "Spectators" then -- checking if the child is a team and isnt spectator team
                     TeamsLeft += 1 
                     Winner = Team
              end
       end
       if TeamsLeft == 1 then
             for i,Player in pairs(Winner:GetPlayers()) do
                     RemoteEvent:FireClient(Player) -- Change RemoteEvent to the event you want to fire
              end
       end
end)

This code replaces the whole team checking thing, by checking for destroyed teams instead of the team’s players. You will need to destroy them for this to work.

This code might need some changes.

1 Like

I might have another idea.

I think we shouldn’t destroy the team, and just set the dead player’s team to spectators, and using the player removed event too. That will be much easier.

1 Like

You can try that, if it doesn’t work, try the code I wrote above.

1 Like

Alright, I’m writing your script right now, Should I remplace everything with it?

For now, maybe you should just disable the old script and make a new one, so if this doesn’t work you can go back to the old one.

1 Like

Don’t worry, the old script is posted here.

1 Like

In that case, you can. But you will still need a script for removing the teams when its empty.

I already did, it is the same from the die detector local script.

game.ReplicatedStorage.Events.RemoveFromTeam.OnServerEvent:Connect(function(player)
	game:GetService("Teams"):WaitForChild(player.Team.Name):Destroy()
	player.Team = game:GetService("Teams"):WaitForChild("Spectators")
end)
1 Like

Alright, your script didn’t work… With no errors. Even though it removes the team, nothing happens.

I think we should use the other script you posted here. I’ll let you know.

1 Like

You could try printing around the script to see where it’s not working, but you can use the other one. Disable the team removing script for that one to work.

1 Like

Okay, so…

All the scripts didn’t work, so I guess I’ll use my old script for now. I’m going to make some changes so it triggers when:

  • The bed value is broken.
  • A team is destroyed.
  • A player got removed from a team.
1 Like

I’m sorry about that, I hope you manage to make your script work. Tell me if I can help with anything else.

1 Like

Alright, I tested this with only one player, and I set all the bedbroken values to true, which means they broke the bed except for my bed. And it worked. But now I’m worried about multiplayer…

-- TEAMS
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")

local spectatorTeam = teams:WaitForChild("Spectators")


-- REMOVED FROM TEAM
game.ReplicatedStorage.Events.RemoveFromTeam.OnServerEvent:Connect(function(player)
	player.Team = spectatorTeam
end)

-- ON PLAYER REMOVED FROM TEAM

redTeam.PlayerRemoved:Connect(function(player)
	if redTeam then
		print("Player removed from the red team")
		if #game.Teams:WaitForChild("Red"):GetPlayers() == 0 and redTeam.EggBroken.Value == true then
			redTeam:Destroy()
			redTeam = nil
			player.Team = spectatorTeam
		end
	end
end)

blueTeam.PlayerRemoved:Connect(function(player)
	if blueTeam then
		print("Player removed from the blue team")
		if #game.Teams:WaitForChild("Blue"):GetPlayers() == 0 and blueTeam.EggBroken.Value == true then
			blueTeam:Destroy()
			blueTeam = nil
			player.Team = spectatorTeam
		end
	end
end)

yellowTeam.PlayerRemoved:Connect(function(player)
	if yellowTeam then
		print("Player removed from the blue team")
		if #game.Teams:WaitForChild("Blue"):GetPlayers() == 0 and yellowTeam.EggBroken.Value == true then
			yellowTeam:Destroy()
			yellowTeam = nil
			player.Team = spectatorTeam
		end
	end
end)

greenTeam.PlayerRemoved:Connect(function(player)
	if greenTeam then
		print("Player removed from the blue team")
		if #game.Teams:WaitForChild("Blue"):GetPlayers() == 0 and greenTeam.EggBroken.Value == true then
			greenTeam:Destroy()
			greenTeam = nil
			player.Team = spectatorTeam
		end
	end
end)

-- ON EGG BROKEN

yellowTeam.EggBroken.Changed:Connect(function()
	if yellowTeam then
		print("Egg and player removed from Yellow team")
		if #game.Teams:WaitForChild("Blue"):GetPlayers() == 0 and yellowTeam.EggBroken.Value == true then
			yellowTeam:Destroy()
			yellowTeam = nil
			
			for i, player in yellowTeam:GetPlayers() do
				player.Team = spectatorTeam
			end
		end
	end
end)

blueTeam.EggBroken.Changed:Connect(function()
	if blueTeam then
		print("Egg and player removed from Blue team")
		if #blueTeam:GetPlayers() == 0 and blueTeam.EggBroken.Value == true then
			blueTeam:Destroy()
			blueTeam = nil
			for i, player in blueTeam:GetPlayers() do
				player.Team = spectatorTeam
			end
		end
	end
end)

greenTeam.EggBroken.Changed:Connect(function()
	if greenTeam then
		print("Egg and player removed from green team")
		if #greenTeam:GetPlayers() == 0 and greenTeam.EggBroken.Value == true then
			greenTeam:Destroy()
			greenTeam = nil
			for i, player in greenTeam:GetPlayers() do
				player.Team = spectatorTeam
			end
		end
	end
end)

redTeam.EggBroken.Changed:Connect(function()
	if redTeam then
		print("Egg and player removed from Red Team")
		if #redTeam:GetPlayers() == 0 and redTeam.EggBroken.Value == true then
			redTeam:Destroy()
			redTeam = nil
			for i, player in redTeam:GetPlayers() do
				player.Team = spectatorTeam
			end
		end
	end
end)

-- GIVE WIN

game.Players.PlayerAdded:Connect(function(player)
	game:GetService("Teams").ChildRemoved:Connect(function()
		if game.ReplicatedStorage.Intermission.MatchStarted.Value == true then
			-- REWARD WIN IF TEAM HAS MORE THAN 1 PLAYER
			task.wait(0.01)
			for i, team in game:GetService("Teams"):GetChildren() do
				if team:IsA("Team") then
					if #team:GetPlayers() >= 1 and team:WaitForChild("EggBroken").Value == false    then
						print(team.Name)
						game.ReplicatedStorage.Events.rewardWin:FireClient(player)
					end
				end

				team:WaitForChild("EggBroken").Changed:Connect(function()
					if #team:GetPlayers() >= 1 and team:WaitForChild("EggBroken").Value == false    then
						print(team.Name)
						game.ReplicatedStorage.Events.rewardWin:FireClient(player)
					end
				end)

				team.PlayerRemoved:Connect(function()
					if #team:GetPlayers() >= 1 and team:WaitForChild("EggBroken").Value == false    then
						print(team.Name)
						game.ReplicatedStorage.Events.rewardWin:FireClient(player)
					end
				end)
			end
		end
	end)
end)
1 Like