function TeamManager.CreateTeams(g)local V=Instance.new("\084\101\097\109",game.Teams)V.TeamColor=BrickColor.new("\066\114\105\103\104\116 \114\101\100")TeamPlayers[V]={}TeamScores[V]=0 V=Instance.new("\084\101\097\109",game.Teams)V.TeamColor=BrickColor.new("\066\114\105\103\104\116 \098\108\117\101")TeamPlayers[V]={}TeamScores[V]=0 end function TeamManager.CreateFFATeam(g)local V=BrickColor.Random()while GetTeamFromColor(V)do V=BrickColor.Random()end local S=Instance.new("\084\101\097\109",game.Teams)S.TeamColor=V TeamPlayers[S]={}TeamScores[S]=0 return S end function TeamManager.AssignPlayerToTeam(V,g)local S local I=math.huge for g,V in pairs(TeamPlayers)do if#V<I then S=g I=#V end end if not Configurations.TEAMS and(not S or#TeamPlayers[S]>0)then S=TeamManager:CreateFFATeam()end table.insert(TeamPlayers[S],g)g.Neutral=false g.TeamColor=S.TeamColor end function TeamManager.RemovePlayer(V,g)local S=GetTeamFromColor(g.TeamColor)local I=TeamPlayers[S]for V=1,#I,1 do if I[V]==g then table.remove(I,V)if not Configurations.TEAMS then S:Destroy()end return end end end function TeamManager.ClearTeamScores(g)for g,V in pairs(Teams:GetTeams())do TeamScores[V]=0 if Configurations.TEAMS then DisplayManager:UpdateScore(V,0)end end end function TeamManager.AddTeamScore(S,g,V)local I=GetTeamFromColor(g)TeamScores[I]=TeamScores[I]+V if Configurations.TEAMS then DisplayManager:UpdateScore(I,TeamScores[I])end end function TeamManager.HasTeamWon(g)for g,V in pairs(Teams:GetTeams())do if TeamScores[V]>=Configurations.POINTS_TO_WIN then return V end end return false end function TeamManager.GetWinningTeam(g)local V=0 local S=nil for g,I in pairs(Teams:GetTeams())do if TeamScores[I]>V then V=TeamScores[I]S=I end end return S end function TeamManager.AreTeamsTied(g)local V=Teams:GetTeams()local S=0 local I=false for g,V in pairs(V)do if TeamScores[V]==S then I=true elseif TeamScores[V]>S then I=false S=TeamScores[V]end end return I end function TeamManager.ShuffleTeams(g)for g,V in pairs(Teams:GetTeams())do TeamPlayers[V]={}end local V=Players:GetPlayers()while#V>0 do local g=math.random(1,#V)local S=table.remove(V,g)TeamManager:AssignPlayerToTeam(S)end end

function TeamManager:CreateTeams()
	local newTeam = Instance.new("Team", game.Teams)
	newTeam.TeamColor = BrickColor.new("Bright red")
	TeamPlayers[newTeam] = {}
	TeamScores[newTeam] = 0
	
	newTeam = Instance.new("Team", game.Teams)
	newTeam.TeamColor = BrickColor.new("Bright blue")
	TeamPlayers[newTeam] = {}
	TeamScores[newTeam] = 0
end

function TeamManager:CreateFFATeam()
	local newTeamColor = BrickColor.Random()
	while GetTeamFromColor(newTeamColor) do
		newTeamColor = BrickColor.Random()
	end
	
	local newTeam = Instance.new("Team", game.Teams)
	newTeam.TeamColor = newTeamColor
	TeamPlayers[newTeam] = {}
	TeamScores[newTeam] = 0
	return newTeam
end

function TeamManager:AssignPlayerToTeam(player)
	local smallestTeam
	local lowestCount = math.huge
	for team, playerList in pairs(TeamPlayers) do
		if #playerList < lowestCount then
			smallestTeam = team
			lowestCount = #playerList
		end
	end
	
	if not Configurations.TEAMS and (not smallestTeam or #TeamPlayers[smallestTeam] > 0) then
		smallestTeam = TeamManager:CreateFFATeam()
	end
	
	table.insert(TeamPlayers[smallestTeam], player)
	player.Neutral = false
	player.TeamColor = smallestTeam.TeamColor
end

function TeamManager:RemovePlayer(player)
	local team = GetTeamFromColor(player.TeamColor)
	local teamTable = TeamPlayers[team]
	for i = 1, #teamTable do
		if teamTable[i] == player then
			table.remove(teamTable, i)
			if not Configurations.TEAMS then
				team:Destroy()
			end
			return
		end
	end
end

function TeamManager:ClearTeamScores()
	for _, team in pairs(Teams:GetTeams()) do
		TeamScores[team] = 0
		if Configurations.TEAMS then
			DisplayManager:UpdateScore(team, 0)
		end
	end
end

function TeamManager:AddTeamScore(teamColor, score)
	local team = GetTeamFromColor(teamColor)
	TeamScores[team] = TeamScores[team] + score
	if Configurations.TEAMS then
		DisplayManager:UpdateScore(team, TeamScores[team])
	end
end

function TeamManager:HasTeamWon()
	for _, team in pairs(Teams:GetTeams()) do
		if TeamScores[team] >= Configurations.POINTS_TO_WIN then
			return team
		end
	end
	return false
end

function TeamManager:GetWinningTeam()
	local highestScore = 0
	local winningTeam = nil
	for _, team in pairs(Teams:GetTeams()) do
		if TeamScores[team] > highestScore then
			highestScore = TeamScores[team]
			winningTeam = team
		end
	end
	return winningTeam
end

function TeamManager:AreTeamsTied()
	local teams = Teams:GetTeams()
	local highestScore = 0
	local tied = false
	for _, team in pairs(teams) do
		if TeamScores[team] == highestScore then
			tied = true
		elseif TeamScores[team] > highestScore then
			tied = false
			highestScore = TeamScores[team]
		end
	end
	return tied
end

function TeamManager:ShuffleTeams()
	for _, team in pairs(Teams:GetTeams()) do
		TeamPlayers[team] = {}
	end
	local players = Players:GetPlayers()
	while #players > 0 do
		local rIndex = math.random(1, #players)
		local player = table.remove(players, rIndex)
		TeamManager:AssignPlayerToTeam(player)
	end
end
