Winner Goes Back To Neutral Team, But Loser Doesn't?

Hey guys, when the loser dies he doesn’t go back to the neutral team. How can I fix this?

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
local Status = ReplicatedStorage:WaitForChild('Status')

while true do
  	local neutralTeam = game:GetService("Teams"):FindFirstChild("Neutral") --Make sure this points to the correct team

	
	--Intermission
	
	local Countdown = 20 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
    wait(0.5)
	local VSpawns = ChosenMap:FindFirstChild('VikingSpawns'):GetChildren() --You need to create this in your map
	local SSpawns = ChosenMap:FindFirstChild('SamuraiSpawns'):GetChildren() --You need to create this in your map
	local KSpawns = ChosenMap:FindFirstChild('KnightSpawns'):GetChildren() --You need to create this in your map

	wait(3) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want

	--teleport the players
	
	local playersInRound = {} -- track alive players
	local teams = {
		[1] = {}, -- Samurai
		[2] = {}, -- Vikings
		[3] = {} -- Knights
	}
	local iterator = math.random(1,3) -- Randomly choose the first team to fill
	local samuraiTeam = game:GetService("Teams"):FindFirstChild("Samurai") --Make sure this points to the correct team
	local vikingsTeam = game:GetService("Teams"):FindFirstChild("Vikings") --Make sure this points to the correct team
	local knightsTeam = game:GetService("Teams"):FindFirstChild("Knights") --Make sure this points to the correct team
	local neutralTeam = game:GetService("Teams"):FindFirstChild("Neutral") --Make sure this points to the correct team
	
	local connections = {} -- store connections to disconnect later
	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			local team = nil
			table.insert(playersInRound, Player) -- add all players to table
			
			--Fill the teams
			if iterator > 3 then
				iterator = 1
			end
			
			if iterator == 1 then
				table.insert(teams[iterator], Player)
				local RandomSpawn = SSpawns[math.random(1, #SSpawns)]
				Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				Player.Team = samuraiTeam
			elseif iterator == 2 then
				table.insert(teams[iterator], Player)
				local RandomSpawn = VSpawns[math.random(1, #VSpawns)]
				Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				Player.Team = vikingsTeam
			else
				table.insert(teams[iterator], Player)
				local RandomSpawn = KSpawns[math.random(1, #KSpawns)]
				Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				Player.Team = knightsTeam
			end
			
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
				
				--Find which team and remove the player from the team
				for i=1, 3 do
					if table.find(teams[i], Player) then
						table.remove(teams[i], table.find(teams[i], Player))
					end
				end
			end)
			
			iterator += 1
		end
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
			--Find which team and remove the player from the team
			for i=1, 3 do
				if table.find(teams[i], player) then
					table.remove(teams[i], table.find(teams[i], player))
				end
			end
		end
	end)
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 60 -- Game Time
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0 or #playersInRound == 0 or (#teams[1] == 0 and #teams[2] == 0) or (#teams[1] == 0 and #teams[3] == 0) or (#teams[2] == 0 and #teams[3] == 0) 

    -- Determine winner and give reward
    if (#teams[2] == 0 and #teams[3] == 0) and #teams[1] > 0 then -- Samurai won
		    local success, message = pcall(function()
	        Status.Value = "Samurai won the game!"
            wait(3)
			local playerList = Players:GetChildren()
			for i=1, #playerList do
				local player = playerList[i]
				if player.Team == samuraiTeam then
		            print("The Winner is: " .. player.Name)
		            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
					player.leaderstats.Denarius.Value = player.leaderstats.Denarius.Value + 100
					player.Team = neutralTeam
				end
			end
        end)
        if not success then
            warn("An error occurred while rewarding winner: " .. message)
        end
    elseif (#teams[1] == 0 and #teams[3] == 0) and #teams[2] > 0 then -- Vikings won
		    local success, message = pcall(function()
			        Status.Value = "Vikings won the game!"
                    wait(3)
			local playerList = Players:GetChildren()
			for i=1, #playerList do
				local player = playerList[i]
				if player.Team == vikingsTeam then
		            print("The Winner is: " .. player.Name)
		            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
					player.leaderstats.Denarius.Value = player.leaderstats.Denarius.Value + 100
					player.Team = neutralTeam
				end
			end
        end)
        if not success then
            warn("An error occurred while rewarding winner: " .. message)
        end
    elseif (#teams[1] == 0 and #teams[2] == 0) and #teams[3] > 0 then -- Knights won
		    local success, message = pcall(function()
	        Status.Value = "Knights won the game!"
            wait(3)
			local playerList = Players:GetChildren()
			for i=1, #playerList do
				local player = playerList[i]
				if player.Team == knightsTeam then
		            print("The Winner is: " .. player.Name)
		            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
					player.leaderstats.Denarius.Value = player.leaderstats.Denarius.Value + 100
					player.Team = neutralTeam
				end
			end
        end)
        if not success then
            warn("An error occurred while rewarding winner: " .. message)
        end
    else
        print("There was no winner.")
        Status.Value = "There was a stalemate this round"
        wait(3)
    end

	--Kill the players
	for _, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	for _, Player in pairs(playersInRound)do
		Player.Team = neutralTeam
		Player:LoadCharacter()
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
local plr = Players:GetChildren()
  plr.Team = neutralTeam
	
	wait(4) -- little pause, make this as long as you want.
	
end
1 Like

In the function connected to the Died event of the humanoid, add this

Player.Team = neutralTeam

You can put it after the for loop, for example.

2 Likes