AnimationTrack Stopping - How would you do this?

Howdy, developers!

Anyway, my issue today is a relatively simple one I believe. I just have no idea what’s going on. I’m trying to make an animation fade in and fade out realistically. While this animation is running, I use KeyframeReached to run some other functions. The fade in works as intended, like so:

animation:Play(0.2, 1, 1)

It fades in for 0.2 seconds. The problem comes in when I try to fade it out again using:

animation:Stop(0.2)

For some reason, the code below never lets the animation actually finish. It seems to either cut off completely or just not even fire the final KeyframeReached that it’s supposed to. The final keyframe, which is crucial for gameplay, happens a little less than 0.2 seconds away from the end of the animation. Is there some super simple logic mistake I’ve made here? Is there a work-around for this? It seems to just ignore all future keyframes once Stop is fired.

while anim.Length - anim.TimePosition > 0.2 do
	wait(0.05)
end
anim:Stop(0.2)

I appreciate any and all responses!

3 Likes

Well instead of that, try

for i = 1, 10 do
    anim:Play(0.2, 1, 1) -- this just forces the animation to play; not required.
end

run = game:GetService('RunService').Heartbeat:Connect(function()
    if anim.Length - anim.TimePosition < 0.2 then
        anim:Stop(0.2)
        run:Disconnect() -- stops the function from running.
    end
end)

I’m not sure if it works.

(Also, happy birthday!)

1 Like

Unfortunately, that solution had no effect.

1 Like

Aw, I’m sorry. I don’t know what else I could try, but (maybe) look around.

:sad:

maybe a function that tells when the animation is done?

Animation.Stopped:wait()

If I’m understanding your problem correctly, it seems your goal isn’t to stop the animation, but rather to make it fade out. If this is correct, then you should be using AdjustWeight.

The Stop method does not “stop” the animation after the fadeTime, but instead stops it instantly, which is probably why your marker is not being reached. I believe you’re interested in the AdjustWeight method, which can do exactly what :Stop() does, except without stopping the animation.

AnimationTrack:AdjustWeight(0, 0.2)

Unrelated to your issue, I would not use a while loop to detect when you’re within 0.2 seconds of the end. I’d use a marker as it’s generally better to use events rather than loops.

4 Likes

Ah, that’s a super useful feature of AdjustWeight. I didn’t know it did that. That’s exactly what I’m after! Thank you so much

2 Likes

I also made a module that lets you apply easing styles when fading animations in or out, since ROBLOX’s is locked to linear. I think this could be of use to you, if you want to check it out it’s here:

1 Like