How would you divide all the players into by two to determine how many teams there will be?

I’m not asking for any scripts just suggestions and ideas. So you would probably get all of the players and then divide it by two to determine the amount of teams right? Also, how would you create the teams? (Example: Server1 had 6 players, so that means 3 teams two players on each team) Thanks!

1 Like

So you have a set number of teams, for example x will be the number of teams you want, x is 4 in this example.

So, to make teams, you could randomize the table Players:GetChildren(), From there, you can iterate through the new randomized table with a for i, v in pairs loop.

Then you can assign teams accordingly using the remainder operator, This is the gist of it:

function RandomTeams()
	local RandomOrderTable = {}

	for i, v in pairs(game.Players:GetPlayers()) do
		if #RandomOrderTable == 0 then
			table.insert(RandomOrderTable, v.Character)
		else
			table.insert(RandomOrderTable, math.random(1, #RandomOrderTable), v.Character)
		end
	end
	
	for i, v in pairs(RandomOrderTable) do
		
		if i % 4 == 0 then
			--Team1
			
			Num4 = Num4 + 1
			return
		end
		
		if i % 3 == 0 then
			--Team2
			
			Num3 = Num3 + 1
			return
		end
		
		if i % 2 == 0 then
			--Team2
			
			Num2 = Num2 + 1
			return
		end
		
		if i % 1 == 0 then
			--Team2
		
			Num1 = Num1 + 1
			return
		end
	end
end

Sorry I just need a little explaining, what does the “if i % 4 == 0 then” do?

the % operator returns the remainder, so if i % 4 == 0 AKA (If i divides into 4 evently then).

say i = 3, then 3 % 4 will return 3,

How would you make that many teams? (For example: 10 players = 5 teams)

You can get the number of players with #game.Players:GetPlayers()

You can calculate the number of teams you want by dividing the number of players by 2, and running math.floor to round it down.

After getting the number of teams, you can create the individual teams using a for loop,

When looping through the number of teams, create a new Team Instance, placing it into teams.

You can place 2 people into that team by getting a list of players using game.Players:GetPlayers(), for each team index you can multiply it by 2 to get the first player on that team, and multiply it by 2, and take 1 from the index to get the second player.

You can team people to that team by changing the Team Property to that team.

add an extra if statement, for 5 teams this would be your script:

function RandomTeams()
	local RandomOrderTable = {}

	for i, v in pairs(game.Players:GetPlayers()) do
		if #RandomOrderTable == 0 then
			table.insert(RandomOrderTable, v.Character)
		else
			table.insert(RandomOrderTable, math.random(1, #RandomOrderTable), v.Character)
		end
	end
	
	for i, v in pairs(RandomOrderTable) do
if i % 5 == 0 then
--team5
end		


		if i % 4 == 0 then
			--Team4
			
			Num4 = Num4 + 1
			return
		end
		
		if i % 3 == 0 then
			--Team3
			
			Num3 = Num3 + 1
			return
		end
		
		if i % 2 == 0 then
			--Team2
			
			Num2 = Num2 + 1
			return
		end
		
		if i % 1 == 0 then
			--Team1
		
			Num1 = Num1 + 1
			return
		end
	end
end
2 Likes

3 % 4 will return 3, not 1. the modulus operator returns the remainder when dividing

1 Like

My bad, i was thinking 4 % 3 instead of 3 % 4

1 Like

Ok so I added a team using Instance.new(“Team”), but how would you parent it to teams?

Also Num4, Num3, Num2, and Num1 are not defined. What would they be defined as?

If you intend to use the team you should create a variable referencing it, and when parenting the team to teams, Instance.new takes a second argument of what you want its parent to be

local team = Instance.new("Team",game.Teams)
1 Like

You don’t need to define Num1, Num2, Num3. Num4 and Num5, they were taken directly from my script.

And yes, you would parent it to teams.

1 Like

How would you place the players in the teams?

We’re currently spoon-feeding you, i’d suggest you learn scripting as these can be easily researched.
You simply do Player.TeamColor = Team.TeamColor…

3 Likes

The weird thing is that even though I parented the team to teams, when I test the game, there isn’t a single team, in teams

Check if teams is not set to neutral

How would you define, player in a server script? Would I use for_, player in pairs(game.Players:GetPlayers()) to define “player”?

EDIT: Never mind I can just do FireAllClients() and in a local script to OnClientEvent and then do game.Players.LocalPlayer and then check

1 Like

Yes that’s how you could go about doing it, Also for future people reading this post, you should mark an answer as a solution

I did something like this:

              ''''
                     local function RandomTeams()
            local RandomOrderTable = {}

            for i, v in pairs(game.Players:GetPlayers()) do
	            if #RandomOrderTable == 0 then
		           table.insert(RandomOrderTable, v.Character)
	            else
		           table.insert(RandomOrderTable, math.random(1, #RandomOrderTable), v.Character)
	       end
                end

            for i, v in pairs(RandomOrderTable) do
               if i % 5 == 0 then
                  local team = Instance.new("Team", game.Teams)
                  game.ReplicatedStorage.Teams:FireAllClients()
               end		


	            if i % 4 == 0 then
		            --Team4
		        local team = Instance.new("Team", game.Teams)
               
		          return
	             end
	
	            if i % 3 == 0 then
		          --Team3
		         return
	           end
	
	          if i % 2 == 0 then
		        --Team2
		       local team = Instance.new("Team", game.Teams)
		        return
	          end
	
	        if i % 1 == 0 then
		     --Team1
	        local team = Instance.new("Team", game.Teams)
		     return
	    end
       end
     end
       else
         print("Classic")

         -- local script in workspace
        local player = game.Players.LocalPlayer

        game.ReplicatedStorage.Teams.OnClientEvent:Connect(function()
              if player.Neutral == true then
             Player.TeamColor = BrickColor.new("Color")
             end
       end) 
                     '''
2 Likes