How to make players swap positions?

I’m making this map where every few seconds, players switch positions. works great with 2 players, however, doesn’t work very well with many players, especially an odd number, with 2 or more players teleporting to the same player. How would I change my code to make it work as intended? As I want players to switch randomly.
Code:

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

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

    @editable
    var Teleporters : []teleporter_device = array{}

    var FortMap : [fort_character]int = map{}

    var PossibleIndexes :[]int = array{0, 1, 2, 3, 4, 5, 6, 7}

    var PlayerIndex : int = 0

    var RiftsCanTeleport : logic = true

    var BreakLoop : logic = false

    var My_Teleporter : teleporter_device = teleporter_device{}


    AssignRifts()<suspends> : void=
        loop:
            for (FortCharacter->Index : FortMap):
                PlayerPosition : vector3 = FortCharacter.GetTransform().Translation
                if (Teleporter := Teleporters[Index]):
                    Teleporter.Enable()
                    if(FortCharacter.IsActive[]):
                        if (RiftsCanTeleport?):
                            if(Teleporter.TeleportTo[PlayerPosition, rotation{}]):
                    else:
                        Teleporter.Disable()

                    
            Sleep(0.016)


    RemoveSelfFromList(Integer:int) : void=
        if(PossibleIndexes.RemoveElement[Integer]):


    SWITCH()<suspends> : void=
        loop:
            Sleep(5.0)
            set RiftsCanTeleport = false
            for (FortCharacter->Index : FortMap):
                if (Indexes := PossibleIndexes.RemoveElement[Index]):
                    RandomIndex := Shuffle(Indexes)
                    for (RIndex : RandomIndex):          
                        if (Agent := FortCharacter.GetAgent[]):
                            if (Teleporter := Teleporters[RIndex]):
                                        
                                Teleporter.Teleport(Agent)
                                set My_Teleporter = Teleporter
                                My_Teleporter.Disable()

            Sleep(3.5)
            set RiftsCanTeleport = true
            My_Teleporter.Enable()
                    
                
    GameBegin()<suspends> : void=
        AllPlayers := GetPlayspace().GetPlayers()
        for (Player : AllPlayers):
            if (FortCharacter := Player.GetFortCharacter[]):
                if (set FortMap[FortCharacter] = PlayerIndex):
                    set PlayerIndex += 1
                    Print("{PlayerIndex}")
                    spawn{AssignRifts()}
                    spawn{SWITCH()}
    
    
    OnBegin<override>()<suspends>:void=
        GameBegin()

first you will want to determine if you have an odd number or an even number which can be done with Mod(x,2) (this is “tell me the remainder of divide arg0/arg1”) where x is the number of players (if the result is zero then you have an even number, and 1 means you have an odd number)

unless you are giving each of the Players a tempIndex variable (this would require inheriting from the Player Character at the least) then there is no real way to check if the shuffle will result in being at the same location, or a different location.
Unless instead of an array of int and assigning an arbitrary sequential list. You populate the list based on some unique characteristic of the Player, worst case you could take their current location (this would mean making an array of vector3)

then after you call shuffle you step through the array and see
if tempArray[ii] == Players[ii].Location
if it is true then either shuffle again(set a limit to how many times this can be attempted otherwise you could get an infinite loop/recursion), or see if multiple match the previous (then swap those).
if this test fails for each one then you would step through the Players and do the assignment to the location of the TempArray value at the same index

the value of this approach is that you have a means to immediately test if they have not been swapped (if that is something you want to avoid) and it has a way to easily swap the values without a temp variable for the swap.

1 Like

I’m not quite sure if I understand, sorry.