Hello @_ari1 how are you?
I’ve been working on this and the code you should implemente is the following:
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Random }
using { /Verse.org }
team_assignment_device := class(creative_device):
# Devices needed
@editable
var MyTimer : timer_device = timer_device{}
@editable
var MyButton : button_device = button_device{}
@editable
TriggerT2 : trigger_device = trigger_device{}
@editable
TriggerT3 : trigger_device = trigger_device{}
# Shuffled array with all players in the Island
var FinalArray : []player = array{}
OnBegin<override>()<suspends> : void =
# Subscribe to the button
MyButton.InteractedWithEvent.Subscribe(StartTimer)
# Subscribe to the Timer's success
MyTimer.SuccessEvent.Subscribe(TeamAssignment)
# Function to start the timer (you can call this whenever you want, in this example I used the button)
StartTimer(Agent:agent): void=
MyTimer.Start()
# Function to change a Player's team
ChangePlayerTeam(Player : player, NewTeamIndex : int) : logic =
if:
# Get all the teams on the island
TeamCollection := GetPlayspace().GetTeamCollection()
Teams := TeamCollection.GetTeams()
# Check if the team we are trying to assing the players exists
NewTeamIndex >= 0
NewTeamIndex < Teams.Length
# Set the new team we will assign to the player
NewTeam := Teams[NewTeamIndex]
then:
# Assign the player to the new team
if(TeamCollection.AddToTeam[Player, NewTeam]):
true
else:
false
# Function to change all the corresponding players to a new team
TeamAssignment(Agent:?agent) : void =
# Get all players in the island and store them in AllPlayers array
AllPlayers := GetPlayspace().GetPlayers()
# Shuffle the previous array and store the new order in FinalArray
set FinalArray = Random.Shuffle(AllPlayers)
# Get the FianlArray lenght and change the result to float type so we can use it later
ArrayLenght : float = FinalArray.Length * 1.0
# Iterate all the players in FinalArray to assign them to a team
for ( Index := 0..FinalArray.Length - 1 ):
if ( Player := FinalArray[Index] ):
# Get all the teams in the island
TeamCollection := GetPlayspace().GetTeamCollection()
# Get the team for the player in the current Index
if ( PlayersTeam := TeamCollection.GetTeam[Player] ):
# Create teams array
Teams := TeamCollection.GetTeams()
# Check if teams array lenght is more than 1, otherwise we don't want to change teams
if (Teams.Length > 1):
# Assign team index 0 to Team1 variable
if ( Team1 := Teams[0] ):
# Check if the player is part of the Team 1 (team index 0)
if (PlayersTeam = Team1):
# Send the first half of the array to team 2 and the second hal to team 3
if ( Index * 1.0 < ArrayLenght * 0.5 ):
# Send player to team index 1 (Team 2)
ChangePlayerTeam(Player, 1)
else:
# Send player to team index 2 (Team 3)
ChangePlayerTeam(Player, 2)
I put comments through all the code so you can understand what everything does.
Hope it helps!