How to make script choose teams randomly and equally?

Hello, I’m to make game blue vs red. I made lobby, map etc. but now, I want to make whenever round starts script chooses randomly teams for players and distribute people equally. and also when round ends, everyone switches team to “Lobby” team. Thank You For Reading!

2 Likes

Before I start scripting, do you have a Level value or Wins value?

3 Likes

What do you mean? (also thank you for response)

2 Likes

If you want the teams balanced, you need some sort of value to go off of to organize teams

1 Like

can you make like whenever round starts, if there’s 12 players in server each red and blue teams get 6 players.

for that, just divide the number of players by two and team half to blue and the rest to red. you can do this in a for loop, using the modulus operator with the index.

the code would look something like this (would explain more but its very late)

local playerList = {"Player1","Player2","Player3","Player4"}

local function DivvyPlayers(players)
	local dividedPlayers = {
		RedTeam = {},
		BlueTeam = {}
	}
	
	for index, username in players do
		if index % 2 == 0 then -- 2 to half the players
			table.insert(dividedPlayers.RedTeam, username)
		else
			table.insert(dividedPlayers.BlueTeam, username)
		end 
	end
	
	return dividedPlayers
end

local divviedplayers = DivvyPlayers(playerList)
print(divviedplayers)

Output:
image

1 Like

Thank you for response and your time, but I did not understand this code and I got some questions. I will ask the questions and reply whenever you’d like.

  1. why playerlist says “player1” “player2” etc. and not maybe like:

Local PlayerList = game:getservice("Players"):GetChildren

  1. how it will work if there’s for example 11 players? I don’t think this script got function to still randomize players to teams.
1 Like

the code is for you to use as a reference. I didn’t use Players service because that would mean I need four people, or four test clients, which I didn’t want to do. you can randomise the list of players by scrambling around the indexes, sorting them in some way, et cetera.

experiment and learn!

Here is an example of how you could do this, this is a very simple example on which you could easily add on to (i got lazy halfway through)

-- Services --

local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

-- Variables --

local RoundTime = 10 -- Round's time in minutes
local RoundTimeSeconds = 30 -- Round's time in minutes

local IntermissionTime = 0 -- Lobby time in minutes
local IntermissionTimeSeconds = 5 -- Lobby time in seconds

local RedTeamCurrent = {} -- Players in red team
local BlueTeamCurrent = {} -- Players in blue team
local plrs = {} -- List of players

-- Teams --

local Lobby = Teams:WaitForChild("Lobby") -- Replace with your lobby team
local BlueTeam = Teams:WaitForChild("Blue") -- Replace with your blue team
local RedTeam = Teams:WaitForChild("Red") -- Replace with your red team

-- Runtime --

local function PlayerAdding(plr : Player)

	print(string.format("Player (%s) has joined the server!", plr.Name)) -- Debug

table.insert(plrs, plr.Name)

	plr.Team = Lobby

end

local function PlayerRemoving(plr : Player)

	print(string.format("Player (%s) has left the server!", plr.Name)) -- Debug

	if table.find(plrs, plr.Name) then table.remove(plrs, table.find(plrs, plr.Name)) end
	
end

-- Enables player added and removing events

Players.PlayerAdded:Connect(PlayerAdding)
Players.PlayerRemoving:Connect(PlayerRemoving)

task.wait(3) -- Waits for game to load

local function DivideTeams() -- Very unorginazed, didnt have time to make it too complex
	
	for _, plr in plrs do -- loops through all players
		
		if #RedTeamCurrent > #BlueTeamCurrent then
			
			table.insert(BlueTeamCurrent, plr)
			
		elseif #BlueTeamCurrent > #RedTeamCurrent then
			
			table.insert(RedTeamCurrent, plr)
			
		else
			
			local random = math.random(1,2)
			
			if random == 1 then
				
				table.insert(RedTeamCurrent, plr)
				
			elseif random == 2 then
				
				table.insert(BlueTeamCurrent, plr)
				
			end
			
		end
		
	end
	
end

local function StartGame()
	
	for _, plr in plrs do
		
		local player = Players:GetPlayerByUserId(Players:GetUserIdFromNameAsync(plr))
		
		if table.find(RedTeamCurrent, plr) then
			
			player.Team = RedTeam
			
		elseif table.find(BlueTeamCurrent, plr) then
			
			player.Team = BlueTeam
			
		else
			
			player.Team = Lobby
			
		end
		
	end
	
	task.wait(RoundTime*60 + RoundTimeSeconds)
	
end

local function Startup() -- Startup
	
	for _, plr in plrs do
		
		Players:GetPlayerByUserId(Players:GetUserIdFromNameAsync(plr)).Team = Lobby -- Gets the plr from name and sets their team to the lobby
		
	end
	
	task.wait(IntermissionTime*60 + IntermissionTimeSeconds)
	
	print("Started")
	
	DivideTeams()
	
	StartGame()
	
end

while true do -- Loops until server closes
	
	Startup()
	
end
1 Like

Thank you so much! it works but, can you help me with making so that for example you are selected to be in red team, you teleport to RoundTeleport_Red and if blue RoundTeleport_Blue, and get back when round is finished to RoundFinishedTeleport.

Oh wait, I changed some lines and now it works completely fine. Code:

	-- Services --
 
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
 
-- Variables --
 
local RoundTime = 0 -- Round's time in minutes
local RoundTimeSeconds = 1 -- Round's time in seconds
 
local IntermissionTime = 0 -- Lobby time in minutes
local IntermissionTimeSeconds = 1 -- Lobby time in seconds
 
local RedTeamCurrent = {} -- Players in red team
local BlueTeamCurrent = {} -- Players in blue team
local plrs = {} -- List of players
 
-- Teams --
 
local Lobby = Teams:WaitForChild("Lobby") -- Replace with your lobby team
local BlueTeam = Teams:WaitForChild("Blue") -- Replace with your blue team
local RedTeam = Teams:WaitForChild("Red") -- Replace with your red team
 
-- Runtime --
 
local function PlayerAdding(plr : Player)
    print(string.format("Player (%s) has joined the server!", plr.Name)) -- Debug
    table.insert(plrs, plr.Name)
    plr.Team = Lobby
end
 
local function PlayerRemoving(plr : Player)
    print(string.format("Player (%s) has left the server!", plr.Name)) -- Debug
    if table.find(plrs, plr.Name) then 
        table.remove(plrs, table.find(plrs, plr.Name)) 
    end
end
 
-- Enables player added and removing events
Players.PlayerAdded:Connect(PlayerAdding)
Players.PlayerRemoving:Connect(PlayerRemoving)
 
task.wait(3) -- Waits for game to load
 
local function DivideTeams() -- Improved team assignment logic
    for _, plr in plrs do -- loops through all players
        if #RedTeamCurrent <= #BlueTeamCurrent then
            table.insert(RedTeamCurrent, plr)
        else
            table.insert(BlueTeamCurrent, plr)
        end
    end
end
 
local function StartGame()
    for _, plr in plrs do
        local player = Players:GetPlayerByUserId(Players:GetUserIdFromNameAsync(plr))
        if table.find(RedTeamCurrent, plr) then
            player.Team = RedTeam
        elseif table.find(BlueTeamCurrent, plr) then
            player.Team = BlueTeam
        else
            player.Team = Lobby
        end
    end
    task.wait(RoundTime * 60 + RoundTimeSeconds)
end
 
local function Startup() -- Startup
    for _, plr in plrs do
        Players:GetPlayerByUserId(Players:GetUserIdFromNameAsync(plr)).Team = Lobby -- Gets the plr from name and sets their team to the lobby
    end
    task.wait(IntermissionTime * 60 + IntermissionTimeSeconds)
    print("Started")
    DivideTeams()
    StartGame()
end
 
while true do -- Loops until server closes
    Startup()
end