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/Player Spawns and Teams|Player Spawns and Teamsif you're not familiar with setting up teams.
To keep track of groups of friends, start by including a
ServerScriptService. In its code, start by including a function that checks if any friends are currently in game, then connect it to the
This code does the following:
- On line 1, the
friendGroupstable 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
friendGroupsare queried with
Player/IsFriendsWith|Player:IsFriendsWith(). Each group containing at least one friend of the incoming player are stored in the
- 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
Assigning Player Teams
If players are with friends, they should initially be placed on the same team. To do this:
TeamServiceto 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 named
assignInitialTeam()which accepts parameters for the player and their potential group. If the function receives a group argument and the group isn’t unfairly large, the
Player/Teamproperty 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
With friend groups already determined, add a
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
This function does the following:
- To keep teams balanced with relatively equal sizes of friend groups,
friendGroupsis sorted from largest group to smallest group (line 90).
- Once sorted,
friendGroupsis iterated through again. If any groups are larger than half the game’s
Players/MaxPlayersvalue, 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
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.