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