Struggling to make a door animation start from the door's current position and not the start (finding the best time position so there is no cut)

I want to make it so the closing animation of a door starts from the last position of the opening animation before stopping it.

The problem is that the closing animation starts from the start (so there is a bit of a teleport)
Test.wmv (239.8 KB)

I have tried saving the time position of the previous animation and applying it to the new one but since the animations slow down near the end it is still not quite what I want. I don’t know what to do.

Here are the functions for opening and closing the doors.

function BowlerTrainClass:open_doors()
	for i, Instance_ in pairs(self.Train:GetDescendants()) do
		if Instance_.Name == 'Door' and Instance_:FindFirstChild('HumanoidRootPart') then
			local AnimationController = Instance_.AnimationController
			local Opening = Instance_.Opening
			local Opened = Instance_.Opened
			local OpeningTrack = AnimationController:LoadAnimation(Opening)
			local OpenedTrack = AnimationController:LoadAnimation(Opened)
			local Sound = Instance_.DoorSound
			
			while wait() do
				if OpeningTrack.Length ~= 0 then
					break
				end
			end
			
			local sound_length = Sound.TimeLength
			local animation_length = OpeningTrack.Length
			local ratio = animation_length / sound_length * 1.5

			for j, Track in pairs(AnimationController.Animator:GetPlayingAnimationTracks()) do
				Track:Stop()
			end
			
			OpeningTrack:Play()
			
			Sound:Play()
			
			OpeningTrack:AdjustSpeed(ratio)
			
			OpeningTrack.Stopped:Connect(function()
				OpenedTrack:Play()
			end)
		end
	end
end


function BowlerTrainClass:close_doors()
	for i, Instance_ in pairs(self.Train:GetDescendants()) do
		if Instance_.Name == 'Door' and Instance_:FindFirstChild('HumanoidRootPart') then
			local AnimationController = Instance_.AnimationController
			local Closing = Instance_.Closing
			local Closed = Instance_.Closed
			local ClosingTrack = AnimationController:LoadAnimation(Closing)
			local ClosedTrack = AnimationController:LoadAnimation(Closed)
			local Sound = Instance_.DoorSound

			while wait() do
				if ClosingTrack.Length ~= 0 then
					break
				end
			end

			local sound_length = Sound.TimeLength
			local animation_length = ClosingTrack.Length
			local ratio = animation_length / sound_length * 1.5
			
			for j, Track in pairs(AnimationController.Animator:GetPlayingAnimationTracks()) do
				Track:Destroy()
			end

			ClosingTrack:Play()

			Sound:Play()

			ClosingTrack:AdjustSpeed(ratio)

			ClosingTrack.Stopped:Connect(function()
				ClosedTrack:Play()
			end)
		end
	end
end

I appreciate your support.

So the animations end up slowing down near the end but they still start from the previous time?

In this case, the simple solution is to make use of AdjustSpeed().

Let’s say you wanted to start the animation halfway through the animation sequence. You’d then wanna double the speed by calling Animation:AdjustSpeed(2) or something like that.

Sorry if I did not explain it well, but essentially what I want is that when the doors opening animation is halfway through and the close_doors() method is called, I don’t want the closing animation to start from it’s beginning, I want it to start it from halfway of the opening animation.

So this works, but it’s just that the animations are slowed down? Or does it start from the beginning when you try to do this, too?

Its because the animations are not linear, saving the previous time position is of no use.

I animated it so the doors go a bit fast then slow down just like real-life train doors
.

Why not use a simpler system with a PrismaticConstraint and an Unachored sliding door?
That way you don’t need to try to stop, start, and ‘align’ animations.
If you use the Servo SlidingBallConstraint | Roblox Creator Documentation for the Prismatic then you can change the TargetPosition to whatever you want, whenever you want and it will try to reach the target. That way if the door is halfway closed (or open) and you click on it you can change the TargetPosition to the open target (or closed).
You can also set the SlidingBallConstraint | Roblox Creator Documentation to make the door accelerate and decelerate however you want it to.

There are a few reasons for this. I want to make my train system easy to use and I wouldn’t write different code to different opening/closing styles. Another reason is that if the doors are open as the train is going at a high speed (which it shouldn’t), the door can glitch out of place and fly after the train. Another reason is that PrismaticConstraint is only limited to one axis of movement and can’t go forward or back. I am using sounds with the doors, and for that, It would be difficult to figure out the best Speed and LinearResponsiveness to create a style that perfectly fits the timeframe of the sounds. I have tried this system before and it was hard to use. I have tried Tweening the doors as well but that also requires writing code for different styles and tweening sequences.

Right now, if I am going to use an Animation system, the best thing I can think of is going through each frame of the animation track and finding the one that is the closest to the door’s current position and then skipping to that time position.