Help with assigning teams with the least amount of players

Hi there, thank you for looking at my post.

I want to assign players to a teamwith the least amount of players; I don’t know where to start. Additionally, If all teams have at least 1 player or an equal amount of players, I want them randomly added to one of them.

Here is what I have so far.

local function randomizeTeam()
	local randomTeam = TeamsService:GetChildren()[math.random(1, #TeamsService:GetChildren())]
	return randomTeam
end

game.Players.PlayerAdded:Connect(function(player)
	for i, player in pairs(game.Players:GetPlayers()) do
		player.Team = randomizeTeam()
	end
end)

Thank you for your help.

1 Like
local function chooseSmallestTeam()
    --  Count how many players are in each team
    local players = game.Players:GetPlayers()
    
    local teams = TeamsService:GetChildren()
    local teamCount = {}
    for i,v in pairs(teams) do
        teamCount[v] = 0
    end
    for i,plr in pairs(players) do
         teamCount[plr.Team] += 1
    end

    -- Find Smallest Team
    local smallestTeamIndex = 1
    local smallestTeamAmount = math.huge
    local count = 0
    for i,team in pairs(teamCount) do
        if team < smallestTeamAmount then
            smallestTeamAmount = team
            smallestTeamIndex = i
        end
        if team == 1 then
            count += 1
        end
    end
    -- If all values are 1 (or 0) then choose random team
    if count == #teamCount then
        return teams[math.random(1,#teams)]
    end
    return smallestTeamIndex
end


game.Players.PlayerAdded:Connect(function(player)
    player.Team = chooseSmallestTeam()
end)

local TeamsService = game:GetService("Teams") -- Define the Teams Service
local function assignPlayerToTeam(player)
	local teams = TeamsService:GetChildren() -- Get all teams
	local minPlayerCount = math.huge -- Set initial minimum player count to a very large number
	local teamsWithLeastPlayers = {} -- Table to store teams with the least players
	
	-- Find the team(s) with the least amount of players
	for _, team in ipairs(teams) do
		local playerCount = #Teams[team.Name]:GetPlayers() -- Get the number of players in the team
		if playerCount < minPlayerCount then
			minPlayerCount = playerCount -- Update the minimum player count
			teamsWithLeastPlayers = {team} -- Store the current team as the only team with the least players so far
		elseif playerCount == minPlayerCount then
			table.insert(teamsWithLeastPlayers, team) -- Add the current team to the list of teams with the least players
		end
	end
	
	player.Team = teamsWithLeastPlayers[math.random(1, #teamsWithLeastPlayers)] -- Assign the player to a random team from the teams with the least players
end

game.Players.PlayerAdded:Connect(assignPlayerToTeam) -- Connect the PlayerAdded event to the assignPlayerToTeam function

Thanks! I had to change line 10 to this because teams was TeamsService:GetChildren

local playerCount = #TeamsService:FindFirstChild(team.Name):GetPlayers()

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