How can I cancel an Event:Wait()?

For my game, I use OOP for the different NPC enemies that each inherit different attack animations/behaviour. Often, the player would kill them while their attack animations is still running. This is a problem because I put hitbox detection code after animation marker signal waits, like this:

animation:GetMarkerReachedSignal("Impact"):Wait()

I guess I’m really just wondering if this will automatically be cancelled if the NPC dies, because often I notice that the hitbox detection code doesn’t run when they die, which suggests this :Wait() is just waiting forever, which also suggests a potential memory leak (in which I am concerned about).

1 Like

You cannot cancel it. What you can do is structure your framework through connections and disconnect as needed, or simply terminate the entire thread altogether (if applicable) with coroutine.running() and task.cancel.

1 Like

So does this mean I shouldn’t be using :GetMarkerReachedSignal():Wait()? If so, how else should I time the animation hitboxes?

via connections.

local impactMarkerConnection: RBXScriptConnection? = nil

-- somewhere
impactMarkerConnection = animation:GetMarkerReachedSignal("Impact"):Once(function(): ()
    impactMarkerConnection = nil

    -- code to run
end)

-- stop your connection(s) somewhere else, like the humanoid dying
if impactMarkerConnection ~= nil then
    impactMarkerConnection:Disconnect()
    impactMarkerConnection = nil
end
2 Likes

Wow. That’s crazy and honestly quite smart. Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.