Assigning a Player to a Team By Random

Hey ive been trying for a couple of days now and i couldnt figure out how to assign players to a team by random after adding the players in-game to a table.

    local plrs = {}
    	
    	for i, player in pairs (game.Players:GetPlayers()) do 
    		if player then 
    			table.insert(plrs,player)--Add each players into plrs table
    		end
	end
4 Likes

There are many ways to separate players into teams. One simple way to achieve this is to shuffle the table and then take half of that table into one team and the other half to another. Here’s how will you will do so.

  • Shuffle function
function shuffle(arr) --This was stolen from another DevForum post. I cannot remember where I got it from.
    local arrCopy = {unpack(arr)}

    for i = 1, #arr do
        arr[i] = table.remove(arrCopy, math.random(#arrCopy))
    end
    return arr
end

Using this function, we can then take the players and separate them into two teams.

  • Separating the players
local Team_One = {}
local Team_Two = {}

--INSERT SHUFFLE FUNCTION HERE

local players = {}
for i,v in pairs(game.Players:GetPlayers()) do --Insert current players to table
    table.insert(players, v.Name)
end

local randomPlayers = shuffle(players) --Randomize players table

for i = 1, math.ceil(#randomPlayers/2) do --Take half of the random players and put them into team 1
    table.insert(Team_One, randomPlayers[i])
    table.remove(randomPlayers, i)
end

for i, v in pairs(randomPlayers) do --Put the rest of the players into team 2
    table.insert(Team_Two, v)
end
randomPlayers = {} --Reset table
12 Likes

Can you put a play in a team without having a Team in the Teams Folder?

Yep! You can handle teams just using tables.

How could i test if this works(seeing what player is in what team)? By using print() ?

NeverMind i Did this to see what team a player is in:

for i = 1, math.ceil(#randomPlayers/2) do --Take half of the random players and put them into team 1
    table.insert(Team_One, randomPlayers[i])
	print(randomPlayers[i],"is in Team One") -- Prints What team the player is in
    table.remove(randomPlayers, i)
	
end

for i, v in pairs(randomPlayers) do --Put the rest of the players into team 2
    table.insert(Team_Two, v)
	print(randomPlayers[i], "is in Team Two") --Prints what team the player is in
end
randomPlayers = {} --Reset table
		
		wait(GameTime)
		end
	end

Here’s what I did lol I did this like yesterday:

function api:AssignRandomTeams()
	local plrs = game.Players:GetPlayers()
	local slotsLeft = #plrs
	local approxPerTeam = math.floor(slotsLeft/2)
	local numbersUsed = {}
	for i, plr in ipairs(plrs) do		
		local randNum = math.random(1,slotsLeft)
		if numbersUsed[randNum] then
			while numbersUsed[randNum] do
				randNum = math.random(1,slotsLeft)
			end
		end
		numbersUsed[randNum]=plrs[i]
	end
	for i, v in ipairs(numbersUsed) do
		v.Neutral = false
		if i > approxPerTeam then
			v.Team = game.Teams.Red
		else
			v.Team = game.Teams.Blue
		end
	end
end
7 Likes

How would I use this for three teams?