Need help with random team generator

For example if there were 3 players in the game there would be a chance that the 3 players get put on the same team. How could i make it so that it balances so if there was 3 players 1 goes on the blue team and then 2 go on red team and if there was 2 players 1 would go on blue team and 1 would go on red team

1 Like
MapChosen = Maps[math.random(1,#Maps)]
		MapChosen.Parent = game.Workspace
		for i,v in pairs(game.Players:GetPlayers()) do
			local HomeValue = game.Teams.Home:GetPlayers()
			local AwayValue = game.Teams.Away:GetPlayers()
			local Team = game.Teams:GetChildren()[math.random(1,#game.Teams:GetChildren())]
			v.Team = Team

This is my code.

Hello, try this:

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

MapChosen = Maps[math.random(1, #Maps)]
MapChosen.Parent = workspace
for _, v in pairs(players:GetPlayers()) do
    local HomeValue = teams.Home:GetPlayers()
    local AwayValue = teams.Away:GetPlayers()
    if #HomeValue > #AwayValue then
        v.Team = teams.Away
    elseif #AwayValue > #HomeValue then
        v.Team = teams.Home
        v.Team = math.random(1, 2) == 1 and teams.Away or teams.Home

1 Like

Store the teams in an array and while looping through the players do:

local teams = Teams:GetChildren() -- or an array of teams

for index, player in Players:GetPlayers() do
    player.Team = teams[(index % #teams) + 1]

For more randomization, you can look into algorithms that shuffle arrays to shuffle the players from :GetPlayers

1 Like

Thank you this works now like i expected it to work.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.