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).
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.
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