How To Edit This Round System So As To Assign One Of 2 Teams To Each Player

Hey guys, in my game there are 3 teams. I would like to make it so that with my round system, each round two teams are chosen and the players are assigned into the two teams evenly. I am stuck though; I have no idea what to do. Could you guys give me some help? Here is my code:

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
local Status = ReplicatedStorage:WaitForChild('Status')

while true do
	
	--Intermission
	
	local Countdown = 20 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
    wait(0.5)
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()


	wait(3) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want
	local Knife = ServerStorage.Knife
	--teleport the players
	
	local playersInRound = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			local RandomSpawn = Spawns[math.random(1, #Spawns)]
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
			local newKnife = Knife:Clone()
			newKnife.Parent = Player.Backpack

			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 60 -- Game Time
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0 or #playersInRound == 1

    -- Determine winner and give reward
    if #playersInRound == 1 then
        local success, message = pcall(function()
            local player = playersInRound[1]
            print("The Winner is: " .. player.Name)
            Status.Value = player.Name .. " won the game!"
            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100
        end)
        if not success then
            warn("An error occurred while rewarding winner: " .. message)
        end
    else
        print("There was no single winner.")
        Status.Value = "There was no single winner this round."
    end

	--Kill the players
	for _, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	for _, Player in pairs(playersInRound)do
		Player:LoadCharacter()
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(4) -- little pause, make this as long as you want.
	
end

Thanks guys, if you need more info just reply. Help is extremely appreciated.

1 Like

Just use math.random() like assigning randomly a team to the player.

1 Like

Yeah, but I’m stuck with the bit of choosing two teams and getting the players in those teams.

Does anyone here know a solution to this problem or have a better idea of it? @DaffyDavinko

Still searching for some help with this one! I’m making a round based PVP game!

I suggest you create a loop that loops through all players then assign them all to a team using the modulus function. For example, if you have 4 teams, and you loop through 5 players, it will result in these calculations:

Player 1 = 4 mod 1 = Team 1
Player 2 = 4 mod 2 = Team 2
Player 3 = 4 mod 3 = Team 3
Player 4 = 4 mod 4 = Team 0
Player 5 = 4 mod 5 = Team 1

Hope this helps,
-Tom :slight_smile:

1 Like

Thanks for your help so far! If anyone has anything they can add, please do!

If you want the basics on how to do it, here ya go.

local team1plrcount = 6
local team2plrcount = 0



local function team1()
for i, v in pairs(game.Players:GetChildren()) do
wait(0.5)
local randompick = math.random(1, --half of current player count)

if randompick == i then 
v.Team = -- Whatever you like
v.TeamColor = -- Whatever you like
end


if i = -- half of player count
      then
   team1plrcount = 0
     end
            end
                   end

Edit: Not sure if it’ll work as I’ve never did this before.

1 Like

Although this method would work, what’s stopping the randomiser from putting all players on the same team?

Statistically it’s a very low chance but it’s not impossible! As it’s polynomially distributed, doing randomisers is a very unreliable way of equalling teams.

1 Like

I haven’t tested this, however, this would be how I would probably do it. Take all of the players, shuffle them randomly, and then assign the first half to one team, and the other half to the other team.

function randomizeTeams(teamColor1,teamColor2);
	local players		= game:GetService("Players"):GetPlayers();
	
	--shuffle the table
	for x=#players,2,-1 do
		local y = math.random(i);
		players[i],players[j] = players[j],players[i];
	end
	
	--place the players onto their respective teams
	for i,player in pairs(players) do
		if i <= math.ceil(#players/2) then
			player.TeamColor = teamColor1;
		else
			player.TeamColor = teamColor2;
		end
	end
end
1 Like

Now, where would I put that inside my round system code in order to make it work? @Crowdsource

Does anyone know where I would insert the above code into my pre existing code in order for it to work properly? Help is really appreciated. Thanks :smiley:

Try reading this API reference

1 Like

Hmm, sadly that didn’t really help :frowning: I really need some help with this question… If you could help that’d be great.