I need help. How to unsubscribe the players when they don't have the item?

In the method you’re using you check to see if the players holding the card, and then you start a subscription. A subscription will go until cancelled, so it’s no longer checking that the players holding the card, its just checking to see if they’re crouching.
Also because you’re running it in a loop you’re building up a bunch of these subscriptions so it’s going to trigger each additional subscription per second of gameplay that you have going (this is a rather large problem, the longer this goes on for the worse the memory issues will get).

Take a look at this aproach:

invisibility_manager := class(creative_device):

    @editable
    ItemDetector: conditional_button_device = conditional_button_device{}
    
    OnBegin<override>()<suspends>:void=
        AllPlayers:=GetPlayspace().GetPlayers()
        for(Player:AllPlayers):
            if(FortCharacter := Player.GetFortCharacter[]):
                FortCharacter.CrouchedEvent().Subscribe(GoInvisibleOnCrouche)
        
        
    GoInvisibleOnCrouche(FortniteCharacter:fort_character, IsCrouched : logic) : void =
        if( IsCrouched=true , Agent:=Fort.GetAgent[] , ItemDetector.HasAllItems[Agent] ):
            FortniteCharacter.Hide()
        else:
            FortniteCharacter.Show()
     

So now instead of creating a loop that subscribes, we’re just doing the single subscribe per player. You really did fundamentally have the right idea of how it should work logically, every second you would check to see if the player was crouching, and if they were holding the card they’d go invisible. That is different than subscribe actually works though.
Its a tool built to do the “crouch checking” for you, and will signal the subscribed function every time you crouch. We set up a “listener” for the trigger so it does it automatically for us once we subscribe to it.
So in the method above we don’t have to run any loop, instead we simply get the trigger for crouching subscribed to first, then whenever that event triggers we check if the players holding the card.

2 Likes