Stop an animation after a certain percentage of it's length

Howdy! :smiley:

I am trying to figure out a way to stop an animation after it has reached a certain percentage of it’s length - for instance 30%. I can’t use a wait() function since it has a minimum cap of ~0.036 seconds. Any ideas?

1 Like

Are you for some reason in need of stopping an animation before 0.03 seconds? To note at 60 fps the delay between each frame is 0.016, which is almost half of the minimum time for a wait.

Also, you could try looking at this:

Add a marker via to your AnimationTrack at the position AnimationTrack.TimePosition * percentage
I am slightly confused on the API for the markers, but I think they are what you need.

1 Like

If you want to stop an animation, use a marker as described above. When the marker is reached, you can do :AdjustSpeed(0) on the animation and it will pause. I’ve done it in the past a lot and in my opinion is the easiest way to pause animations at a certain time.


I have almost zero experience with markers and keyframes. This is an excerpt of my code, which does not seem to work so far:

local keyframe ="Keyframe", animationTrack)
local marker ="KeyframeMarker")
marker.Name = "Stop"
marker.Value = animationTrack.Length * 0.3
	print("Marker reached")

Try creating the KeyFrameMarker and the animation VIA the animation editor

1 Like

Aaah I see, but I want to be able to edit the time position of the event in the animation, as it’s desired position is a variable depending on the player’s input. Is there any way I could access this event through a script?

Maybe, use the animation editor to create the marker and change the value with a script. Also, make sure you are using a server script, cause local scripts won’t replicate

1 Like

Yeah, but how do I access the marker from a script? I have been searching around on the Developer Hub for quite a while now and I can’t find any information :frowning:

animation events are located in 1 of the keyframes, change the name of that keyframe and you will be able to access the event

Yes, but what would the code look like? I tried using WaitForChild() but that doesn’t work.

Anyone know how to access these events?

Well, I think if you use LoadAnimation(), it will return the animation track with the keyframes, from there, you could use the keyframe the marker is a descendant of via WaitForChild() (remember to change the name of the keyframe). After that, you could use WaitForChild() again to access the marker. Finally, you could adjust the marker to the desired value.

1 Like

I still don’t get it, there’s still an infinite yield in the output. Using :WaitForChild() doesn’t work on the animation, neither the animationTrack.

 local animation ="Animation")
 animation.AnimationId = ""
 local animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(animation)
 animationTrack:WaitForChild("Hit") -- Infinite Yield

it’s located in a keyframe, not the animation, so, lets say we changed the keyframe the marker is in to “markerKeyframe” and the marker’s name is “Hit”, the code will look like this

local animation ="Animation") -- animation to be loaded
animation.AnimationId = "" -- animation ID
local animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(animation) -- load the animation
local markerKeyframe = animationTrack:WaitForChild("markerKeyframe") -- the keyframe the marker is in
local marker = markerKeyframe:WaitForChild("Hit") -- get the marker
1 Like

The keyframe is already named “Hit”, and the :WaitForChild() function returns an infinite yield. Also, as far as I am aware, I can only insert keyframes and animation events via the Animation Editor, not markers.

then the only way is to probably use wait(numberOfSeconds), or create multiple animations, but that would be too much work. I would just recommend using wait() and then stopping the animation after the desired length using AnimationTrack:Stop() rather than using a marker

1 Like

Hmm, yeah the only problem with wait() is that I want the animation to stop faster than the minimum wait() cap :confused:

Are you sure, cause the minimal wait cap is just under 0.13 seconds?

1 Like

Yeah, as the animations I use may last as low as 0.1 secs, the event needs to fire between 0-0.1 secs :confused: I’ve tested it and the delay is too large

Then tween the part yourself (via CFrame:Lerp()) using the the Motor6d’s transform property (could use C0 and C1, it would affect other animations, if that is what you want), if you would like a code sample, I could show you, as the situation you are in would need a more custom solution than roblox animations

1 Like