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