How to have one team get more players?

for _, player in pairs(game.Players:GetPlayers()) do
	if player then
		if selectedGamemode.Name == 'Classic' then
			local taggerTeamPlayerCount = #teams.Red:GetPlayers()
		    local runnerTeamPlayerCount = #teams.Blue:GetPlayers()
		    if runnerTeamPlayerCount < taggerTeamPlayerCount then
				player.Team = teams.Blue
				giveScripts(player, 'Runner')
			elseif taggerTeamPlayerCount < runnerTeamPlayerCount then
			   	player.Team = teams.Red
				giveScripts(player, 'Tagger')
			else
				local coinFlip = math.random()
				if coinFlip < 0.5 then
					player.Team = teams.Red
					giveScripts(player, 'Tagger')
				else
					player.Team = teams.Blue
					giveScripts(player, 'Runner')
				end
			end
		end
    end
end

This is my script to currently get an even amount of taggers and runners. But what I want is to separate it so there’s only a certain amount of taggers depending how many players in the game.

My logic would be, if theres 2 players, it’d be 1 tagger, 1 runner. 3 players, 1 tagger, 2 runners.

Would continue as one tagger until theres between say 5-8 players, then there’d be 2 taggers. Then 9-12 could be 3 taggers, then 13-16 would be 4, but I have no clue how to work all this out in as little space as possible

This is the method I used in my work-in-progress Freeze Tag game, although I’m fairly certain there’s a much better way of doing it.

local RunnerTaggerRatio = 2 -- 2 runners for ever Tagger
local TotalPlayers = #(game.Players:GetChildren())

local TaggerCount = math.floor((TotalPlayers/(RunnerTaggerRatio +1)) + 0.5) -- Hard to explain, but this divides the number of players by the ratio+1 to get the total number of taggers and then rounds the number to the nearest integer

local RunnerCount = math.floor(((TotalPlayers/(RunnerTaggerRatio +1))*RunnerTaggerRatio ) + 0.5) -- This gets the number of taggers and multiplies it by the ratio to get the number of runners and then rounds the number

while TaggerCount + RunnerCount > TotalPlayers do -- Because of the rounding, we might end up with too many players.
     RunnerCount = RunnerCount - 1 -- If there's too many players, subtract one from the runner count
end

if TaggerCount < 1 then -- If for whatever reason there's no taggers, then move one from a runner to a tagger
     TaggerCount = TaggerCount + 1
     RunnerCount = RunnerCount - 1
end

Now you have the number of players required for each category (the RunnerCount and TaggerCount variables), you can use a loop to do whatever you need to team the players!

1 Like

If that just gets the amount of runners and taggers then how do I implement it to set the set runners as blue team and the set taggers as red team

This script should choose the correct number of runners and taggers (based on the ratio) and then assign these to random players and change their team and give them scripts… Hopefully :stuck_out_tongue:

function randomizeTable(tbl) -- A very useful function from StickMasterLuke
	math.randomseed(tick())
	local returntbl={}
	if tbl[1]~=nil then
		for i=1,#tbl do
			table.insert(returntbl,math.random(1,#returntbl+1),tbl[i])
		end
	end
	return returntbl
end

local RunnerTaggerRatio = 2 -- 2 runners for ever Tagger
local TotalPlayers = #(game.Players:GetChildren())

local TaggerCount = math.floor((TotalPlayers/(RunnerTaggerRatio +1)) + 0.5) -- Hard to explain, but this divides the number of players by the ratio+1 to get the total number of taggers and then rounds the number to the nearest integer

local RunnerCount = math.floor(((TotalPlayers/(RunnerTaggerRatio +1))*RunnerTaggerRatio ) + 0.5) -- This gets the number of taggers and multiplies it by the ratio to get the number of runners and then rounds the number

while TaggerCount + RunnerCount > TotalPlayers do -- Because of the rounding, we might end up with too many players.
     RunnerCount = RunnerCount - 1 -- If there's too many players, subtract one from the runner count
end

if TaggerCount < 1 then -- If for whatever reason there's no taggers, then move one from a runner to a tagger
     TaggerCount = TaggerCount + 1
     RunnerCount = RunnerCount - 1
end

local playerList = randomizeTable(game.Players:GetPlayers()) -- Gets the player list and randomizes the order

for i, player in pairs(playerList) do
	if player then
		if selectedGamemode.Name == 'Classic' then
			if i <= TaggerCount then
				-- Player has been chosen as a tagger
				player.Team = teams.Red
				giveScripts(player, 'Tagger')
			else
				-- Player has been chosen as a runner
				player.Team = teams.Blue
				giveScripts(player, 'Runner')
			end
		end
	end
end

This code is untested but hopefully will work :joy:

I’ll stress that this isn’t the best or most efficient way of doing what you need, but it’s the best I can come up with :smile:

1 Like