We use cookies on this site to enhance your user experience

Pausing An Animation When a KeyFrame is Reached

Pausing An Animation When a KeyFrame is Reached

Sep 07 2018, 2:25 AM PST

The example below pauses any AnimationTrack playing on the humanoid when a keyframe named ‘PausePlayback’ is reached.

humanoid.AnimationPlayed:Connect(function(animationTrack)
	-- make sure the track exists
	if not animationTrack then
		return
	end
	local connection
	-- connect the KeyframeReached event
	connection = animationTrack.KeyframeReached:Connect(function(keyframeName)
		if keyframeName == "PausePlayback" then
			-- adjust speed to 0, pausing the animation
			animationTrack:AdjustSpeed(0)
		end
	end)
	-- disconnect the KeyframeReached event when the animation finishes
	animationTrack.Stopped:Wait()
	connection:Disconnect()
end)