Help with Team switch script

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

3 Likes

Yea, you found the right script, you can change this code to change how each player gets assigned a team.

1 Like

What exactly do you want to change?

1 Like

i want players choose the team they want i got that codes but idk how to use it with this one

local players = game:GetService(‘Players’)
local plr = players.LocalPlayer
local plrGui = plr:WaitForChild(‘PlayerGui’)
local mainGui = game:GetService(‘ReplicatedStorage’):WaitForChild(‘Teampick’)
mainGui.Parent = plrGui
local frame = mainGui:WaitForChild(‘Frame’)
local button1 = frame:WaitForChild(‘BlueButton’)
local button2 = frame:WaitForChild(‘RedButton’)
local Teamswitch = game:GetService(‘ReplicatedStorage’):WaitForChild(‘Teamswitch’)

button1.MouseButton1Down:Connect(function()
Teamswitch:FireServer(1)
mainGui.Enabled = false
end)

button2.MouseButton1Down:Connect(function()
Teamswitch:FireServer(2)
mainGui.Enabled = false
end)

and this one

local players = game:GetService(‘Players’)
local Teamswitch = game:GetService(‘ReplicatedStorage’):WaitForChild(‘Teamswitch’)
local teams = game:GetService(‘Teams’)
local team1 = teams:WaitForChild(‘Blue’)
local team2 = teams:WaitForChild(‘Red’)

players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
repeat
task.wait()
until plr.Character
local humanoid = plr.Character:WaitForChild(‘Humanoid’)
humanoid.Died:Connect(function()
task.wait(players.RespawnTime)
plr:LoadCharacter()
end)
end)
end)

Teamswitch.OnServerEvent:Connect(function(plr,data)
if plr.Team == teams.Choosing then
if data ==1 then
plr.Team = team1
else
plr.Team = team2
end
plr:LoadCharacter()
end
end)

So you need help implementing this into your game?

yeah so the players can pick their own teams instead of letting game auto pick for them

Okay sure. First off delete that team script that was automatically done by roblox. Second, how experienced are you with roblox studio in general?

i tried to delete team script and game didn’t start well that’s why i just want to change the code that responsible of auto pick

I’m good with Roblox studio but not that good + can’t make scripts by myself and still learning

anyone can help ?

my request is simple … making players choose their own team instead of letting game force them into teams