despawn pets

I want to make the pet despawn when a button is pressed, but i do not know how to. I tried to figure it out, but i cant. Here is the code if anyone is willing to help:

spawn_pet_device2 := class(creative_device):
    var Players : [ ]player = array{}
    @editable
    SpawnEgg : conditional_button_device = conditional_button_device{}

    @editable
    DebbyAsset : creative_prop_asset = DefaultCreativePropAsset
    
    
    SpawnDebby( Agent : agent ) : void =
        if (FortCharacter := Agent.GetFortCharacter[]):
            PlayerPos := FortCharacter.GetTransform().Translation
            PlayerForward := FortCharacter.GetViewRotation().GetLocalForward() * 300.0
            DebbySpawnPos := PlayerPos + PlayerForward
            
            Result := SpawnProp(DebbyAsset, DebbySpawnPos, rotation{})
            if (SpawnedProp := Result(0)?):
                spawn:
                    PetFollowsPlayer(FortCharacter, SpawnedProp)
                    
    PetFollowsPlayer( FortniteCharacter : fort_character, CreativeProp : creative_prop)<suspends> : void =
        loop:
            Sleep(0.0)
            PlayerPos := FortniteCharacter.GetTransform().Translation
            DebbyPos := CreativeProp.GetTransform().Translation

            DistanceFromPlayer := Distance(PlayerPos, DebbyPos)
            Time := DistanceFromPlayer / 350.0
            CreativeProp.MoveTo(PlayerPos, rotation{}, Time)

    OnBegin<override>()<suspends>:void=
        set Players = GetPlayspace().GetPlayers()
        SpawnEgg.ActivatedEvent.Subscribe(SpawnDebby)

Hi!

Despawning as in removing it? Then I would use the “Dispose” function. This disposes the object. Any reference to it can be later checked for “IsDisposed” to ensure that no more “MoveTo” functions are called on the disposed object.

1 Like

i do not know how to implement this into the code, but ty for the advice

In your example, it would simply be “CreativeProp.Dispose()”
Same as you would use the “MoveTo”

If you want a separate function to despawn it, you would have to save the reference at the top of the class so you can access it there (or pass it through the function).