Good day, fellas. So I was working on an animation and I wanted it to be paused at the last frame when the key is being held down and play another animation [the release] animation when the key is released.
I did, (Just a replica of what I made, since the original one can be complex.
UIService.InputBegan:connect(function(inputKey) if inputKey.KeyCode == Enum.KeyCode.Q then anim = humanoid:LoadAnimation(folder["Animation"]) wait(1) anim:AdjustSpeed(0) end end
I tried doing this but later realized that it's probably not a very efficient way of doing it, since I'll be doing it on several animations as well, with different time length (I was thinking the wait(1)
would be changed everytime and it also feels un-optimized to me having the wait. So, detecting whether the animation is at its last keyframe, how should I do that? What event of the animation can get the length of the animation I'm playing?
I am using the Moon Animator 2 for Animating it. So, I really hope it doesn't interfere that much on this problem. What do you recommend? (Not asking for a script, rather a recommendation of a solution to this)
There are some ways of checking the last keyframe. First you should try
AnimationTrack.Stopped:Connect(function)
but sometimes it actually waits the animation stops and do the function after it, making it perform kindda ugly; so you can use AnimationEvents, creating an event in the last keyframe before the end of your anim, and to listen to it you use
AnimationTrack:GetMarkerReachedSignal(function)
Or in last case maybe you could try AnimationTrack.Changed event, like
AnimationTrack.Changed:Connect(function(property) if property = TimePosition and AnimationTrack.TimePosition == time.Length*0.9 then -- do ur stuff end end)
but I don't know if this last one will work.
Links to help working with animation events:
https://developer.roblox.com/en-us/articles/using-animation-editor
https://developer.roblox.com/en-us/api-reference/function/AnimationTrack/GetMarkerReachedSignal
In last case, you can use the deprecated form of animation events, that were the keyframe reached event, try serching about it.
Maybe you will have to face a new problem while trying those out, that is the memory leak caused by connecting an event inside another event. Instead of doing this:
UserInputService.InputBegan:Connect(function() if keycode == x then AnimationTrack:GetMarkerReachedSignal(function) -- if you pressed the input 10 times, you created 10 connections, so it will listen to the event 10 times and more as much you press. When it is 1k the game will start to freeze. end end)
Do this
local holding = false UserInputService.InputBegan:Connect(function() if keycode == x then holding = true end end) UserInputService.InputEnded:Connect(function() if keycode == x then holding = false end end) AnimationTrack:GetMarkerReachedSignal(function() if holding == true then -- do ur stuff end end)
Else, to make an animation freeze in a certain point (after calling the events) you can set the AnimationTrack.TimePosition to any number and stop the animation through AnimationTrack:AdjustSpeed(0)