Run function on player leave and check its team

I’m creating a car/parkour game, but if the player leaves, the car is still there, obscuring the way. To clarify, I want a function that detects if a player has left and checks its team so I can reset everything for that team. That would also mean other people can join the game.

I want a function that first knows when a player has left
Currently I do that with: GetPlayspace().PlayerRemovedEvent().Subscribe(OnPlayerLeft)

But then I want to check what team it was for so I can reset everything for that team. (Team limit is 1)

How can I do that? Thanks for reading.

You could get the team and index for the player as they leave:

Team := GetPlayspace().GetTeamCollection().GetTeam[Player]
TeamIndex := GetPlayspace().GetTeamCollection().GetTeams().Find[Team]

Then use that to restart stuff for each team:

case (TeamIndex):
    0 =>
        CarTeam1.Reset()
        OtherTeam1.Reset()
    1 =>
        CarTeam2.Reset()
        OtherTeam2.Reset()
    2 =>
        CarTeam3.Reset()
        OtherTeam3.Reset()
    3 =>
        CarTeam4.Reset()
        OtherTeam4.Reset()
    _ => {}

Thanks for your response, I have implemented your suggestion but I get 2 errors at [Player] and [Team]

Here is my full code:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/Playspaces }

detect_player_left := class(creative_device):
    OnBegin<override>()<suspends>:void=
        GetPlayspace().PlayerRemovedEvent().Subscribe(OnPlayerLeft)

    OnPlayerLeft(Player:player):void=
        Team := GetPlayspace().GetTeamCollection().GetTeam[Player]
        TeamIndex := GetPlayspace().GetTeamCollection().GetTeams().Find[Team]

        case (TeamIndex):
            0 =>
                Print("Team 1: Resetting Cars and Other Objects")
            1 =>
                Print("Team 2: Resetting Cars and Other Objects")
            2 =>
                Print("Team 3: Resetting Cars and Other Objects")
            3 =>
                Print("Team 4: Resetting Cars and Other Objects")
            4 =>
                Print("Team 5: Resetting Cars and Other Objects")
            _ =>
                Print("Unknown Team: No Reset Actions Defined")

Error Message:

This invocation calls a function that has the ‘decides’ effect, which is not allowed by its context. The ‘decides’ effect indicates that the invocation calls a function that might fail, and so must occur in a failure context that will handle the failure. Some examples of failure contexts are the condition clause of an ‘if’, the left operand of ‘or’, or the clause of the ‘logic’ macro.

You could do:

OnPlayerLeft(Player:player):void=
    if:
        Team := GetPlayspace().GetTeamCollection().GetTeam[Player]
        TeamIndex := GetPlayspace().GetTeamCollection().GetTeams().Find[Team]
    then:
        case (TeamIndex):
            ...
1 Like

Thank you! You’re a legend! :100:

1 Like