Pick A Random Player With Verse

Hello, i need help, im trying to build some kind of an Among Us map, and im trying to get a code that picks a random players and changes their team to team 2.

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Random }

# A Verse-authored creative device that can be placed in a level
Team_manager := class(creative_device):

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        AllPlayers:= GetPlayspace().GetPlayers() # Collect players available in the map
        TeamCollection:= GetPlayspace().GetTeamCollection() # Collect the team collection
        ActiveTeams:= TeamCollection.GetTeams() # Collect active teams in the map

        if (
                Index:= GetRandomInt(0, AllPlayers.Length-1) # Select random number in range of total players
                RandomPlayer:= AllPlayers[Index] # The randomly selected player
                TeamCollection.AddToTeam[RandomPlayer, ActiveTeams[1]] # Add the randomly selected player to the desired team (team 2 in this case)
            ):

I have this code but it doesn’t work good, can anybody help me?

What behavior do you notice exactly? is the “if” just failing? try putting an else with a print and see if that’s the cause. The code seems relatively correct to me, except I would have just shuffled the array itself and then picked whatever was at index 0. Maybe trying to swap teams exactly on onbegin is not 100% certain to work, would putting a sleep(0.1) or something make this code less prone to fail? other than that I have no other idea on what could cause it to not work.

Well, it does work, i forgot to explain that this code is part of something bigger, I would be very grateful if you send me a DM on discord.

pizzasteve4848

thats my discord tag.

If you dont want to see the full thing, heres a summary:

When the game starts, we have a code that 1 players turns into a “monster”. The problem is that the monster is always going to be the first person to load on the island. We want the monster to be only in team 2, but we cant make it.

Another solution we could do is that the monster could be activated by a trigger, but i don’t have the enough knowledge in Verse to do that.

try replacing the if part with this

if(PlayerToBeMonster:=Shuffle(AllPlayers)[0],TeamCollection.AddToTeam[PlayerToBeMonster,ActiveTeams[1]]){}

(post deleted by author)

Okay so i think the code works. But the monster still loads to the first person on the island and idk how to fix that (nor idk what causes it)
here is the code if you wanna look at it

using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }

See Create Your Own Device Using Verse | Unreal Editor for Fortnite Documentation | Epic Developer Community for how to create a verse device.

A Verse-authored creative device that can be placed in a level

IA_1 := class(creative_device):

@editable Inactivo : cinematic_sequence_device = cinematic_sequence_device{}
@editable Correr : cinematic_sequence_device = cinematic_sequence_device{}

@editable Cuerpo : creative_prop = creative_prop{}

# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=
    AllPlayers := GetPlayspace().GetPlayers()
    if (Player := AllPlayers[GetRandomInt(0, AllPlayers.Length -1)], Agent := agent[Player], Fort := Agent.GetFortCharacter[]):
        Fort.Hide()
        Correr.Play(Agent)
        Fort.SprintedEvent().Subscribe(OnMoved)
        spawn:
            UpdateCharacter(Fort, vector3{Z:=-70.0, Y:=0.0})

UpdateCharacter(Player : fort_character, Offset : vector3)<suspends>:void=
    loop:
        Sleep(0.0)
        NewPos := Player.GetTransform().Translation + Offset
        if (Cuerpo.TeleportTo[NewPos, Player.GetTransform().Rotation]) {}


OnMoved(MoveData : tuple(fort_character, logic)):void=
    if (Agent := MoveData(0).GetAgent[]):
        if (MoveData(1)?):
            Inactivo.Stop(Agent)
            Correr.Play(Agent)
        else:
            Correr.Stop(Agent)
            Inactivo.Play(Agent)