Hello ,
so I’m making an pvp game this game is already posted by Roblox in Roblox studio it called Capture The Flag , what i want to do is change how teams switch is because this game already has script that can do auto Assign players to team .
i want to change that and make it players can pick their own teams im not sure really what script does auto assign because i don’t know much about scripting and that what i saw idk if that’s the right script that needs to be changed
local TeamManager = {}
– ROBLOX services
local Teams = game.Teams
local Players = game.Players
– Game services
local Configurations = require(game.ServerStorage.Configurations)
local DisplayManager = require(script.Parent.DisplayManager)
– Local variables
local TeamPlayers = {}
local TeamScores = {}
– Initialization
for _, team in ipairs(Teams:GetTeams()) do
TeamPlayers[team] = {}
TeamScores[team] = 0
end
– Local Functions
local function GetTeamFromColor(teamColor)
for _, team in ipairs(Teams:GetTeams()) do
if team.TeamColor == teamColor then
return team
end
end
return nil
end
– Public Functions
function TeamManager:ClearTeamScores()
for _, team in ipairs(Teams:GetTeams()) do
TeamScores[team] = 0
DisplayManager:UpdateScore(team, 0)
end
end
function TeamManager:HasTeamWon()
for _, team in ipairs(Teams:GetTeams()) do
if TeamScores[team] >= Configurations.CAPS_TO_WIN then
return team
end
end
return false
end
function TeamManager:GetWinningTeam()
local highestScore = 0
local winningTeam = nil
for _, team in ipairs(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 ipairs(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: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
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)
return
end
end
end
function TeamManager:ShuffleTeams()
for _, team in ipairs(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
function TeamManager:AddTeamScore(teamColor, score)
local team = GetTeamFromColor(teamColor)
TeamScores[team] = TeamScores[team] + score
DisplayManager:UpdateScore(team, TeamScores[team])
end
return TeamManager