# Source URL: https://dev.epicgames.com/documentation/en-us/uefn/team-multiplayer-balance-in-verse
# Local doc: epic-docs/documentation/en-us/uefn/team-multiplayer-balance-in-verse.md
# Section: Complete Script
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
team_multiplayer_balance := class(creative_device):
# Holds the teams found with GetTeams()
var Teams : []team = array{}
OnBegin<override>()<suspends> : void =
Print("Verse Device Started!")
set Teams = Self.GetPlayspace().GetTeamCollection().GetTeams()
AllPlayers := GetPlayspace().GetPlayers()
#Subscribe to PlayerAddedEvent to allow team rebalancing when a new player joins the game
Self.GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerAdded)
Print("Beginning to balance teams")
BalanceTeams()
#Handles a new player joining the game
OnPlayerAdded(InPlayer : player) : void =
Print("A new Player joined, assigning them to a team!")
BalanceTeams()
<#
For each player, find the number of players of the team they're on. Iterate through the
list of teams and assign them to the team with the least amount of players, or their
starting team in case of ties.
Verse Library
verse
11 Complete Script
Manages team assignment and rebalances players automatically when joining or starting.
extracted-snippets/documentation/en-us/uefn/team-multiplayer-balance-in-verse/11-complete-script.verse
Sign in free to read the full source 🌴
This Verse Library file is members-only. Create a free account to view the complete source and download the .verse file.