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

function TeamManager:CreateTeams()
	local newTeam = Instance.new("Team", game.Teams)
	newTeam.TeamColor = BrickColor.new("Really red")
	TeamPlayers[newTeam] = {}
	TeamScores[newTeam] = 0
	
	newTeam = Instance.new("Team", game.Teams)
	newTeam.TeamColor = BrickColor.new("Really 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
