Players randomly set on neutral team

I’ve been working on a team organizing system, but sometimes players are randomly added to the neutral team when they arent suppose to.
Code

function Gamemodule.Teams(Map)
	Statuse.Gamemode.Value = "Teams"
	local Team1 = Instance.new("Team")
	Team1.Name = "Red"
	Team1.Parent = game:GetService("Teams")
	Team1.TeamColor = BrickColor.new("Bright red")
	Team1.AutoAssignable = false
	local Team2 = Instance.new("Team")
	Team2.Name = "Blue"
	Team2.Parent = game:GetService("Teams")
	Team2.TeamColor = BrickColor.new("Bright blue")
	Team2.AutoAssignable = false
	local Spawns = Map:FindFirstChild("Spawns")
	for i, spawners in pairs(Spawns:GetChildren()) do
		if spawners:GetAttribute("Red") == true then
			spawners.TeamColor = Team1.TeamColor
			spawners.BrickColor = Team1.TeamColor
			spawners:FindFirstChildWhichIsA("ParticleEmitter").Color = ColorSequence.new{
				ColorSequenceKeypoint.new(0, Team1.TeamColor.Color),
				ColorSequenceKeypoint.new(1, Team1.TeamColor.Color)
			}
			spawners.Neutral = false
		elseif spawners:GetAttribute("Blue") == true then
			spawners.TeamColor = Team2.TeamColor
			spawners.BrickColor = Team2.TeamColor
			spawners:FindFirstChildWhichIsA("ParticleEmitter").Color = ColorSequence.new{
				ColorSequenceKeypoint.new(0, Team2.TeamColor.Color),
				ColorSequenceKeypoint.new(1, Team2.TeamColor.Color)
			}
			spawners.Neutral = false
		end
	end
	
	
	local Debounce = false
	local Teammembers = Players:GetChildren()
	for team = 1, #Teammembers do
		local Random1 = math.random(#Teammembers)
		if Random1 == nil then return end
					local Team1 = game.Teams:FindFirstChild("Red")
			local Team2 = game.Teams:FindFirstChild("Blue")
			if #(game.Teams.Red:GetPlayers()) == #(game.Teams.Blue:GetPlayers()) then
				-- some team randomizer statement if teams are equal
				local number = math.random(1,2)
				if number == 1 then 
				Teammembers[Random1].Team = Team1
				Gamemodule.AddGui(Teammembers[Random1],Color3.new(1, 0, 0))
				else
				Teammembers[Random1].Team =  Team2
				Gamemodule.AddGui(Teammembers[Random1],Color3.new(0, 0, 1))
				end
			elseif #(game.Teams.Red:GetPlayers()) > #(game.Teams.Blue:GetPlayers()) then 
				-- Blue team has less players then red.
			Teammembers[Random1].Team =  Team2
			Gamemodule.AddGui(Teammembers[Random1],Color3.new(0, 0, 1))
			else
				-- Red team has less players then blue - only over occurrence we can get. 
			Teammembers[Random1].Team =  Team1
			Gamemodule.AddGui(Teammembers[Random1],Color3.new(1, 0, 0))
			end
		table.remove(Teammembers,Random1)
	end
	task.wait(3)
	for i, v in pairs(Players:GetChildren()) do
		if v.Team == nil then
			local Team1 = game.Teams:FindFirstChild("Red")
			local Team2 = game.Teams:FindFirstChild("Blue")
			if #(game.Teams.Red:GetPlayers()) == #(game.Teams.Blue:GetPlayers()) then
				-- some team randomizer statement if teams are equal
				local number = math.random(1,2)
				if number == 1 then 
					v.Team = Team1
					Gamemodule.AddGui(v,Color3.new(1, 0, 0))
				else
					v.Team =  Team2
					Gamemodule.AddGui(v,Color3.new(0, 0, 1))
				end
			elseif #(game.Teams.Red:GetPlayers()) > #(game.Teams.Blue:GetPlayers()) then 
				-- Blue team has less players then red.
				v.Team =  Team2
				Gamemodule.AddGui(v,Color3.new(0, 0, 1))
			else
				-- Red team has less players then blue - only over occurrence we can get. 
				v.Team =  Team1
				Gamemodule.AddGui(v,Color3.new(1, 0, 0))
			end
		end
	end
	Events.ShowTeamGui:Fire("On")
end 

Code for ending game

function Gamemodule.EndGame()
	local Teams = game:GetService("Teams")
	local Team = Teams:GetChildren()
	for i, teamg in pairs(Team) do	
		if teamg.ClassName == "Team" then
			teamg:Destroy()
		end		
	end 
	for i, players in pairs(Players:GetChildren()) do
		if players.Character:FindFirstChild("Head") then
			
			if players.Character:FindFirstChild("Humanoid").Health ~= 0 then
				if players.Character:FindFirstChild("Head"):FindFirstChild("OverHeadgui") then
					players.Character:FindFirstChild("Head"):FindFirstChild("OverHeadgui"):Destroy()
				end
			end	
		end
		players:FindFirstChild("Score").Value = 0		
		players.TeamColor = BrickColor.new("White")
		players.Neutral = true
	end
	
	Events.ShowTeamGui:Fire("Off")
	Events.LocalShowTeams:FireAllClients("Off")
	
	for i, player in pairs(Players:GetPlayers()) do
		local playerGui = player:WaitForChild("PlayerGui")
		local screenGui = playerGui:WaitForChild("TeamPoints")
		local frame = screenGui:WaitForChild("Frame")
		frame.Blue.TextLabel.Text = 0
		frame.Red.TextLabel.Text = 0
		frame.Blue.Visible = false
		frame.Red.Visible = false
	end
	
	Statuse.Gamemode.Value = ""
	Statuse.RedPoint.Value = 0
	Statuse.BluePoint.Value = 0
end

Adding Overhead Guis Code

function Gamemodule.AddGui(player,color)
	local Clone = Extras.OverHeadgui:Clone()
	if player.Character:FindFirstChild("Head") then
	Clone.Parent = player.Character:FindFirstChild("Head")
	local Label = Clone.Points
	Label.TextColor3 = color
		return Label
		end
end

Ive tried turning neutral off and setting teams after the game starts again but players are still being set on the neutral team

1 Like

Turn this off
image

1 Like

Wouldn’t that just not allow other players from other teams to spawn? How will it fix players randomly getting set on neutral team?

Are you somehow deleting any team instance?

After the game round ends, all team instances are removed

1 Like

So the player is setting to neutral team in the middle of the game?
Is it only 1 player or everyone?

1 Like

Only 1 player is getting set to neutral team, sometimes it happens when players join right after the game starts and sometimes it happens in the middle of the round

Maybe its this bug.

Do you know any workarounds for this bug?

Poster of that bug report here. If this is that bug (I don’t delete or create any teams for context), I am not aware of any workarounds.

Given that you are adding/removing teams, it’s possible that it may not be that bug.