How to randomize players team?

I am trying to make it so if a player touches a UI button they get put onto one of 2 teams (red and blue) but I want it so the teams stay even. How would I do this?

1 Like
--Change plr if to whatever you have already like plr = game.Players.LocalPlayer
--Teaming will need to happen through server side script via  remote events

script.Parent.MouseEnter:Connect(function()	
	local Red = game.Teams["Bright red Team"]:GetPlayers()
	local Blue = game.Teams["Bright blue Team"]:GetPlayers()
	
	local Choice = math.random(1,2)

	if Choice == 1 then
		if #Red > #Blue then
			plr.TeamColor = BrickColor.new("Really blue")
		end
	else
		if #Red < #Blue then
			plr.TeamColor = BrickColor.new("Really red")
		end
	end
end)

That would randomly select a team, but he wants the teams to have even numbers. Therefore, you would need to use GetPlayers() and track how many are in each team, red and blue. Then you would check which one is <= and add it to that team.

Use math.random and just change their team on function…

script.Parent.MouseEnter:Connect(function()	
	local Red = game.Teams["Bright red Team"]:GetPlayers()
	local Blue = game.Teams["Bright blue Team"]:GetPlayers()
	
	local Choice = math.random(1,2)

	if Choice == 1 then
		if #Red > #Blue then
			plr.TeamColor = BrickColor.new("Really blue")
		end
	else
		if #Red < #Blue then
			plr.TeamColor = BrickColor.new("Really red")
		end
	end
end)

This is someone else’s script from above, but this works, he updated the script so it checks the amount of players.

1 Like