Team Balancing
Team Balancing

Keeping friends together and balancing opposing teams ensures everyone has a fun, fair gameplay experience. By writing code to balance teams, players can become friends over time, encouraging them to stay and play more.
/articles/Table|tables
and Roblox's BindableEvent|BindableEvents
.
/articles/Player Spawns and Teams|Player Spawns and Teams
if you're not familiar with setting up teams.
Grouping Friends
To keep track of groups of friends, start by including a Script
in ServerScriptService
. In its code, start by including a function that checks if any friends are currently in game, then connect it to the Players/PlayerAdded
event.
This code does the following:
- On line 1, the
friendGroups
table will store lists of players who have at least one friend in common. - To find if the incoming player has any friends in the game, players in every group within
friendGroups
are queried withPlayer/IsFriendsWith|Player:IsFriendsWith()
. Each group containing at least one friend of the incoming player are stored in themutualGroups
table. - If multiple groups containing friends are located, those groups are first merged with the
mergeGroups()
helper function (defined in Combining Friend Groups) and then the player is assigned to the group. Otherwise, if no friends are found, the player is assigned to a new group. - Once players have been associated with their online friends, the helper function
assignInitialTeam()
(defined in Assigning Player Teams) is used to place them on a team.
Combining Friend Groups
To combine multiple groups containing mutual friends into one larger group, add the following mergeGroups()
helper function directly above the groupFriends()
function. This function inspects groups of mutual friends, merges them into one group, removes the old separate groups, and then returns the new merged group back to the groupFriends()
function.
Assigning Player Teams
If players are with friends, they should initially be placed on the same team. To do this:
- Get
TeamService
to interact with the game’s teams. It’s recommended to keep services at the top of your scripts.
- Below the
mergeGroups()
function, add a function namedassignInitialTeam()
which accepts parameters for the player and their potential group. If the function receives a group argument and the group isn’t unfairly large, thePlayer/Team
property of the first entry in the friend group is used to assign the team. Otherwise, solo players will fill in the least populated team.
Handling Players Leaving
When a player leaves the game, you need to remove them from the friendGroups
table. Not doing so will cause the game to believe there are larger friend groups in the game than there actually are.
In order to remove the player’s entry, iterate through friendGroups
, searching for a user that matches the player, and set that player’s current index to nil
. If the player’s previous group is then empty, set the group to nil
as well. Finally, make sure you connect the function to the Players/PlayerRemoving
event.
Rebalancing Teams
With friend groups already determined, add a BindableEvent
within ReplicatedStorage
to be used when testing and to rebalance teams when needed. To keep up with Roblox conventions, add this code to the top of the script.
Next, to keep teams balanced with relatively equal sizes of friend groups, add the following balanceTeams()
function below removeFromGroup()
:
This function does the following:
- To keep teams balanced with relatively equal sizes of friend groups,
friendGroups
is sorted from largest group to smallest group (line 90). - Once sorted,
friendGroups
is iterated through again. If any groups are larger than half the game’sPlayers/MaxPlayers
value, those friends are split apart and assigned to smaller teams. Otherwise, the teams are simply sorted in ascending order and the group is assigned to the smallest team.
Now connect the BalanceTeamsEvent
bindable BindableEvent/Event|Event
to the balanceTeams()
function and, to use the balancing functionality, BindableEvent/Fire|Fire
the event. It’s recommended to fire the event between matches, but you might decide on other conditions when team balancing is required.