Need to disconnect GetMarkerReachedSignal function after I'm done with using it multiple times

I’m unsure if this is the title was the right way to phrase it, so I’m hoping the examples below convey what I mean in detail

All the same animation event

local hit_connection

hit_connection = stand_animation:GetMarkerReachedSignal("Hit"):Connect(function() -- hit
       create_sound(8701825353, 0.8, 0.9)
       hit_connection:Disconnect() -- once this is fired the other events wont run, need to bypass this
end)

Any help is greatly appreciated.

3 Likes

I could be wrong, but as far as I understand, if you do this:

stand_animation:GetMarkerReachedSignal("Hit"):Connect(function() -- hit
	create_sound(8701825353, 0.8, 0.9)
end)

And when the Model that holds the Humanoid > Animator > AnimationTrack is destroyed, the connection gets disconnected

If you store the connection inside a variable as you did:

local hit_connection = stand_animation:GetMarkerReachedSignal("Hit"):Connect(function()
     create_sound(8701825353, 0.8, 0.9)
end)

Even if the Model holding the Animator is destroyed the connection still in memory (possibly causing memory leak), and you can manually disconnect it whenever you need. For example, the Model (Player or NPC) is never destroyed but you want to stop listening the MarkerReachedSignal.

The code you are showing just, store the connection into a variable and its disconnected after the first “Hit” marker reached, causing all other markers to not trigger the function connected to it.

3 Likes

I completely forgot destroying the object its connected to will automatically disconnect it, thank you!!

2 Likes

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