I have 3 teams in Teams, lobby, taggers and players. I want to, when the round starts, randomly select a team for them to be on. but, i do not want the taggers team to exceed the number 2. Any help is appreciated, as i am bad at scripting.
local teamSerivce = game:GetService("Teams") -- The team service
game.Players.PlayerAdded:Connect(function(player)
local teams = teamService:GetTeams() -- Thes all the teams and return it to an array (table)
local randomTeam = teams[math.random(1,#teams)] -- Picks up a random team
if randomTeam.Name = "Taggers" then -- If team name is taggers
if #randomTeam:GetPlayers() < 2 then -- If team have people less than 2 then
player.Team = randomTeam
end
else -- If team name is not taggers then
player.Team = randomTeam
end
end)
What if i want instead of PlayerAdded, instead activated by remote event? Like, i want to this repeat once a round has ended, but when it has officially ended, it switches back to the lobby team.
Edit: it works, yet i need that thing i said above.
local CurrentMode = "MODE1"
function SetPlayerTeam(Player, TeamLocation)
if Player and TeamLocation then
Player.Team = TeamLocation
elseif not Player then
warn("Player does not exist.")
elseif not TeamLocation then
warn("Team is missing from Teams Service.")
end
end
function PickTeam()
local Teams = {}
if CurrentMode == "MODE1" then
Teams = {"Team1", "Team2", "Team3"}
elseif CurrentMode == "MODE2" then --Make different if statements depending on the mode.
Teams = {"Lobby"}
end
local LimitedTeam = "Team3"
local LimitedTeamLocation = game:GetService("Teams"):FindFirstChild(LimitedTeam) --Searches for the limited team using the keyword
local MaxPlayersForLimitedTeam = 2
local NewTeam = Teams[math.random(1, #Teams)]
if NewTeam == LimitedTeam then --If the random team is the team you want to limit..
if LimitedTeamLocation then --If the team exists.
if #LimitedTeamLocation:GetPlayers() >= MaxPlayersForLimitedTeam then --Checks the amount of players on that team. If its more than or equal to the max, it will repick.
return PickTeam() --Repicks team
end
else
error('Could not find "' ..tostring(NewTeam)..'" in Teams Service')
end
end
local TeamObject = game:GetService("Teams"):FindFirstChild(NewTeam)
if TeamObject then
return TeamObject --Returns the team object
else
error('Could not find "' ..tostring(NewTeam)..'" in Teams Service')
end
end
function SetTeams()
local PlayerList = game:GetService("Players"):GetPlayers() --Gathers players.
for i,v in pairs (PlayerList) do
SetPlayerTeam(v,PickTeam())
end
end
Event:connect(function() --Define your event here to switch modes and trigger the team change.
CurrentMode = "MODE2" --Change the name of the mode according to the type of event.
SetTeams()
end)
You can change the CurrentMode variable to whatever you want. You just have to clarify the conditions in the PickTeams() function and make sure to set the CurrentMode when an event fires.
For example:
if CurrentMode == "Playing" then
Teams = {"Runners", "Taggers"}
elseif CurrentMode == "Intermission" then
Teams = {"Lobby"}
end
local LimitedTeam = "Taggers"
GameEndedEvent:connect(function() --replace GameEndedEvent with the event fired by the remote event
CurrentMode = "Intermission"
SetTeams()
end)