How to divide players into two teams equally

Hello, I would like to have players divided into two teams equally, I have looked everywhere, but none of the solutions work, I tried to use a for loop to split the players into half, but that didnt work either. I would appreciate any help to my problem, thanks.

2 Likes
local Teams = {
    game:GetService('Teams'):WaitForChild('Team1');
    game:GetService('Teams'):WaitForChild('Team2');
}

local TeamCount = 1;

for _,Player in pairs(game:GetService('Players'):GetPlayers()) do
    Player.Team = Teams[TeamCount];

    TeamCount++;
    if (TeamCount > #Teams) then
        TeamCount = 1;
    end
end

Alternatively if you’re just using 2 teams, you can modulo % the index of the for loop by 2, add one to the value, then use that as the index of the ‘Teams’ table:

local Teams = {
	game:GetService('Teams'):WaitForChild('Team1');
	game:GetService('Teams'):WaitForChild('Team2');
}

for Index,Player in pairs(game:GetService('Players'):GetPlayers()) do
	Player.Team = Teams[(Index%2)+1];
end
2 Likes

Hi, thanks for the reply, I have two teams, and it is putting my players into neutral, instead of the teams

2 Likes

Have you ensured that the game:GetService('Teams'):WaitForChild('Team1'); etc. has been updated correctly to reflect on your game’s teams? It’s working fine in my studio.

3 Likes

yes i have, here is my script:

local Teams = {
	game:GetService('Teams'):WaitForChild('TeamOne');
	game:GetService('Teams'):WaitForChild('TeamTwo');
}

for Index,Player in pairs(game:GetService('Players'):GetPlayers()) do
	Player.Team = Teams[(Index%2)+1];
end

Screenshot 2024-11-28 225742
Screenshot 2024-11-28 225752

1 Like

Are you waiting until the players have actually joined before changing the teams?

1 Like

I am using the local server test with 2 players in studio

1 Like

You’ll need to wait until the Player’s have actually joined. It takes a few seconds.

I’m not sure how you want it to work in your game, is this something you want to update every time a player joins?

1 Like

Yes, everytime someone joins, it puts them in one of the teams

1 Like

I waited 30 seconds, and it hasn’t assigned a team yet, the players are still all on neutral

1 Like
local Teams = {
	game:GetService('Teams'):WaitForChild('Team1');
	game:GetService('Teams'):WaitForChild('Team2');
}

local function PlayerAdded(Player)
	local LowestTeam;
	
	for _,Team in pairs(Teams) do
		if (not LowestTeam) then
			LowestTeam = Team;
		elseif (#Team:GetPlayers() < #LowestTeam:GetPlayers()) then
			LowestTeam = Team;
		end
	end
	
	Player.Team = LowestTeam;
end;

game:GetService('Players').PlayerAdded:Connect(PlayerAdded);
for _,Player in pairs(game:GetService('Players'):GetPlayers()) do
	PlayerAdded(Player);
end

If you only have two teams in your game (or only have teams that you want it to sort against):

local function PlayerAdded(Player) -- Every time a Player joins
	local LowestTeam; -- Initialize a new variable for holding the 'team with the lowest number of players'
	
	for _,Team in pairs(game:GetService('Teams'):GetChildren()) do -- For every team in the game
		if (not LowestTeam) then -- If this is the first loop
			LowestTeam = Team; -- Make this team the lowest team
		elseif (#Team:GetPlayers() < #LowestTeam:GetPlayers()) then -- If this team has less players than the current 'LowestTeam'
			LowestTeam = Team; -- Assign it as the new 'LowestTeam'
		end
	end
	
	Player.Team = LowestTeam; -- Set the player's team to the lowest team
end);

game:GetService('Players').PlayerAdded:Connect(PlayerAdded);
for _,Player in pairs(game:GetService('Players'):GetPlayers()) do
	PlayerAdded(Player);
end
1 Like

Hi, I tested it, the first player joins and puts them in neutral, and when a second player joins it puts them in one of the teams but, one of them is still in neutral

2 Likes

Also when both player joins at the same time, it puts them both at neutral

2 Likes

I’ve updated my original script. That should fix it.

2 Likes

Isn’t there an AutoAssignable property on a Team object?

2 Likes

Also just so you know, the ‘AutoAssignable’ property of teams should do most of this for you.

1 Like

Great minds think alike :ok_hand:

This is useful if you’re intending on having multiple teams but only want a selection of them assignable at set points. I think I’m a bit confused by what the intention is.

2 Likes

when i used AutoAssignable, it puts both two players into same team

1 Like

And AutoAssignable is turned on on both teams?

2 Likes

Make sure your two teams are different colors or else they won’t appear separately on the leaderboard.

3 Likes