Random Team Sorter only works on 1 person

  1. What do you want to achieve?
    For each person to be added to teams properly (specified percentages can be found in Percentages

  2. What is the issue?
    It only seems to be working for 1 person, then it just doesn’t work at all. It says that it no longer needs to assign a player to any team, which is already incorrect.

  3. What solutions have you tried so far?
    I tried rewriting it, and encountered the same issue.

function RoundSystem.MinusPercentage(number, percentage)
	local firstnum = percentage / 100
	local secondnum = 1 - firstnum
	local thirdnum = secondnum * number
	return math.round(thirdnum)
end


function RoundSystem.SetTeams()
	local Percentages = {
		Infected = 50,
		ClassD = 35,
		Foundation = 15,
	}

	local PlayerService = game:GetService("Players")
	local TeamsService = game:GetService("Teams")
	
	local PlayersTable = PlayerService:GetPlayers()
	local Players = {}
	
	for i, value in ipairs(PlayersTable) do
		Players[value] = value
	end
	
	local AmountOfPlayers = #PlayersTable
	
	local Teams = TeamsService:GetTeams()
	local AmountOfTeams = #Teams

	local UnassignedPlayers = {}

	local TeamNumbers = {}

	for i, value in pairs(Percentages) do
		table.insert(TeamNumbers, {Name = i, PlayersNeeded = AmountOfPlayers - RoundSystem.MinusPercentage(AmountOfPlayers, value) }) 
	end
	
	UnassignedPlayers = Players
	

	for i, value in pairs(UnassignedPlayers) do
		print(i, value)
		for i2, value2 in ipairs(TeamNumbers) do
			print(value2.Name)
			if value2.PlayersNeeded == 0 then print("No players needed!") continue end
			for i3, value3 in ipairs(Teams) do
				if value3:GetAttribute("InternalName") == value2.Name then
					value.Team = value3
					value.Character:PivotTo(value3.Spawns:GetChildren()[math.random(1, #value3.Spawns:GetChildren())].Value.CFrame)
				end
			end
			UnassignedPlayers[i] = nil
			value2.PlayersNeeded -= 1
		end
	end
end

Where is this script located? Is it a server or a client script?

Server Script
(char limittyttttttttttt)

Did you try this with more than 1 person? And who got into a team you or the other tester?

I used the multiple client testing, with 2-6, all had 1 person as Infected, the rest as Spectator

Anyone able to help with this?

The problem with your code is that even after an appropriate team has been found (and the player teamed and teleported), it will continue to check the other teams, which will lead to having all teams have a PlayerNeeded value of 0 (I tested with 4 players). The quick and simple fix to this is to add a break statement after the value2.PlayersNeeded -= 1 line so it stops looping and moves onto the next player.

1 Like

Thanks, I’ll try this in a moment.