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