Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a team Auto Balance script?

How would I go about makeing a Team balace script.

team1 = Game.Teams.team1 team2 = Game.Teams.Team2

How can I find the number of players in a team

team1num = 0 team2num = 0

if team1.PlayerAdded then team1num +1

if team2.PlayerAdded then team2num +1

if team1num >= team2num then Player.teamColor = BrickColor.new("team2Color") if team2num >= team1num then Player.TeamColor = BrickColor.new("team1Color")

^^^^ Is that anywhere close to what im looking for?

1 answer

✓ Accepted answer

The teams service should automatically (try to) balance teams when players join but if you wanted to rebalance teams you could write your own function to handle that. Take a look at this wiki page that shows how to rebalance teams -I don't think the actual method works though but it wouldn't hurt to try.

However, to answer your question fully,

function getPlayersOnTeam(teamColor)
	local players = {}
	for _, p in pairs(game.Players:GetPlayers()) do
		if p.TeamColor == BrickColor.new(teamColor) then
			table.insert(players, p)
		end
	end
	return players
end

function getSmallestTeam(teams)
	local numPlayers, smallestTeam = math.huge
	for team, players in pairs(teams) do
		if #players < numPlayers then
			numPlayers = #players
			smallestTeam = team
		end
	end
	return smallestTeam
end

game.Players.PlayerAdded:connect(function(player)
	local reds = getPlayersOnTeam("Bright red")
	local blues = getPlayersOnTeam("Bright blue")
	local greens = getPlayersOnTeam("Bright green")
	local smallestTeam = getSmallestTeam({
		["Bright red"] = reds,
		["Bright blue"] = blues,
		["Bright green"] = greens
	})
	player.TeamColor = BrickColor.new(smallestTeam)
end)

Whenever a player joins, the code above will put them on the smallest team out of the Bright red, Bright blue and Bright green teams.

4 comments

0

Awsome Thank you, Really helped me.

0

Same

0

Same

0

Same

This discussion is preserved as it was. New answers, comments, and votes are closed.