Stopped event of AnimTrack don't fire when i need

I’m making gate system using animations. I have LocalScript which is firing thru RemoteEvent to run animation smooth for every user (to let users have no bugs like gate stopped in incorrect place).

I need to make action code which will run after animationtrack is finished. However using animTrack.Stopped event didn’t give any effect. This event unreasonably fires right after animation starts, but not when it stops.

Here’s code:

--(values not included)
game.ReplicatedStorage.GateAnimationEvent.OnClientEvent:Connect(function(gate,state)
	
	function reactafteranim (path,state)
		print("Animation kind of stopped")
		-- After animation finishes block code of code
	end
	
	path = gate.Parent
	local animationTrack
	if state == 'Close' then
		animationTrack = gate:LoadAnimation(animationOpen)
		animationTrack:Play()
		animationTrack.Stopped:Connect(reactafteranim(path,state))
	else
		animationTrack = gate:LoadAnimation(animationClose)
		animationTrack:Play()
		animationTrack.Stopped:Connect(reactafteranim(path,state))
	end
end)


Picture shows - the code that has to execture after animation finished - executes during animation.

Perhaps you could put a KeyframeLabel near the end of the point in which you want to stop. Then use GetMarkerReachedSignal(…) to get where the keyframe label position.

This is quite accurate but if you want accuracy you could do the following,

delay(animationTrack.Length,function()
    reactafteranim(path,state))
end)

If this is not what you want/mean feel free to respond :slight_smile:

1 Like

you can use debounce.Put the script inside the debounce. so if it plays,make the debounce false and set it true after the AnimationTrack.Completed:Wait() line

Try using animationTrack.Stopped:Wait() instead of connecting to it every single time that remote is fired. That’s most likely going to cause a memory leak and might be causing your problem, otherwise the code is too vague. Also, why do you constantly reload the animations every time when the remote is fired? It would be better if you loaded both of them once outside of the connection, and then just play them when you need them to play.


local closedAnim = gate:LoadAnimation(animationClose)
local openAnim = gate:LoadAnimation(animationOpen)

if state == 'Close' then
		openAnim:Play()
		openAnim.Stopped:Wait()
               reactafteranim(path, state)
	else
		closedAnim:Play()
		closedAnim.Stopped:Wait()
                reactafteranim(path, state)
	end

Sorry if indentation is bad, I’m on mobile.

Thanks for replying and answering me.
Also i loadanimation every time event fires due to some other problems in full code (i showed only part of it) that are solved only this way. I’m sure I’ll find way to fix this too, anyway thanks for replying

1 Like