Manipulating animations forward & backwards

Hello,
I’m currently attempting to do just a simple switch animation for my track, however I’m unable to properly manipulate the animation in coding. It either returns to the default position, or it’ll pause briefly, but after reversing it it doesn’t seem to work again.

I’ve tried using time position & KeyframeReached / GetMarkerReachedSignal, however as a beginner scripter, I don’t really understand them. I’ve been coding this with the hopes of the animation being played inside the function I’ve already cobbled together. Currently I’m trying AnimationTrack.Stopped:Wait() but like I said before the animation just returns to the original position.

local Switched = script.Parent:FindFirstChild("Switched")

local Aa = script.Parent:FindFirstChild("Aa")
local Ba = script.Parent:FindFirstChild("Ba")

local Button = script.Parent:FindFirstChild("Button")
local Sound = script.Parent.Button:FindFirstChild("Sound")

local LArrow = script.Parent.Host.Att1:FindFirstChild("ArrowA")
local RArrow = script.Parent.Host.Att1:FindFirstChild("ArrowB")

local Ab = script.Parent.Rig:FindFirstChild("B")
local Bb = script.Parent.Rig:FindFirstChild("A")

local Anim = script.Parent.Rig.Animation
local AnimCtrl = script.Parent.Rig.AnimationController
AnimationTrack = AnimCtrl:LoadAnimation(Anim)

AnimationTrack:Play()
AnimationTrack:AdjustSpeed(0)
AnimationTrack.Looped = false

Button.Click.MouseClick:Connect(function(Act)
	
	Switched.Value = not Switched.Value
	if Switched.Value == true then
		
		AnimationTrack:AdjustSpeed(1)
		Sound:Play()
		
		Aa.CanCollide=false
		Ab.CanCollide = false
		Ba.CanCollide = true
		Ba.CanCollide = true
		
		RArrow.Color = ColorSequence.new(Color3.fromRGB(0,255,0))
		LArrow.Color = ColorSequence.new(Color3.fromRGB(255,0,0))
		
		AnimationTrack.Stopped:Wait()
		AnimationTrack:AdjustSpeed(0)
		
		
	else
		AnimationTrack:AdjustSpeed(-1)
		Sound:Play()
		
		Aa.CanCollide=false
		Ab.CanCollide = false
		Ba.CanCollide = true
		Ba.CanCollide = true
		
		RArrow.Color = ColorSequence.new(Color3.fromRGB(255,0,0))
		LArrow.Color = ColorSequence.new(Color3.fromRGB(0,255,0))
		
		wait(.5)
		AnimationTrack:AdjustSpeed(0)
		
	end
end)

How can i go about resolving this? Any & all help is appreciated.

1 Like

To my understanding, is the track in its original position it without an animation? And when you press the button, it moves via an animation? I don’t believe you can play animations in reverse, however if you want it to return you can just stop the animation.

If you pass a number into the stop and play function of an animation, you’ll essentially fade it in/out. For instance since stopping that animation would make the track just teleport back, run the code like this.

AnimationTrack:Stop(1)
1 Like

It does work for return the switch to the original position, however when switching it left, it occasionally returns to the original position unprompted. Should I just extend the animation & stop to pause it in that position without risking it resetting?

1 Like

Sorry for the late response, is the animation looping? And if it isn’t is it literally just moving it from the starting position to the end position? And again if thats the case, then make it constantly loop at the end position and do the same thing with :Stop(), and run it with :Play([desired time here]). I’m just a tad confused on how the system works. Also, I believe using tweens rather than animations would be easier in the case too. I could link you to a few articles if you’re interested in it.

Apologies for the late response as well, I have considered tweens & I think I’ll look into them more. I’m sorry if my explanation was a bit janky. I’m just trying to move the switches from left to right when the button is clicked, and hold them in that position. Some articles for tweens would be very helpful though. Thank you so much!

https://developer.roblox.com/en-us/api-reference/class/Tween

https://developer.roblox.com/en-us/api-reference/function/TweenService/Create

These two are must haves. Basically you create constructors of a tween, they can literally be any property besides binary ones I believe. And you can move them in any way you desire, smooth, clunky, slow fast, etc.