Animations briefly reset when transitioning to another animation

Hi there, I’m currently working on my game and I’ve run into an issue that I can’t seem to fix.

Because my knowledge of animation is fairly limited, I created three separate animations for the doors: one for opening, one for the doors remaining open, and one for closing.

The problem occurs when switching from the opening animation to the “doors open” (idle) animation. For a brief moment, the doors reset, which makes the transition look very awkward and unnatural. Interestingly, switching from the doors-open animation to the closing animation does not cause any noticeable issues.

This issue happens on all of my trains.

Here’s a video showing the problem:

External Media

Here’s a snippet of code handling the animations.

-- animation stuff
local function playAnimations(animationControllers, openSounds, closeSounds, specificDoorLightsTable, doorSide, isClosingDoors, player, trainEvent, playerTrain)
	

	local animations = getTrainAnimations(playerTrain)

	local function playAndStoreAnimation(animationData)
		local animation = Instance.new("Animation")
		animation.AnimationId = animationData.AnimationId
		local animationTracks = {}

		for _, controller in ipairs(animationControllers) do
			local track = controller:LoadAnimation(animation)
			track.Looped = animationData.Loop or false
			track:Play()
			table.insert(animationTracks, track)
		end
		return animationTracks
	end

	if not isClosingDoors then -- opening doors
		-- sounds for opening
		if openSounds and #openSounds > 0 then
			for _, s in ipairs(openSounds) do
				if s then s:Play() end
			end
		end

		-- opening anim
		local openingTracks = playAndStoreAnimation(animations.Opening)
		task.wait(animations.Opening.Duration) -- wait for finito

		-- changing weight, but not sure if this is the correct way
		for _, t in ipairs(openingTracks) do
			t:AdjustWeight(1, 1)
		end

		-- play holding
		local holdingTracks = playAndStoreAnimation(animations.Holding)

		-- store so we can stop them later (keyed per train btw)
		currentlyOpenDoorTracks[playerTrain][doorSide] = holdingTracks

		-- this isn't relevant for the issue
		updatePassengerSystemDoors(playerTrain, doorSide, true)

		printAllDoorStatuses()

	else -- this is for closing
		-- stop any currently playing anim for specific side and train
		local tracksToStop = currentlyOpenDoorTracks[playerTrain][doorSide] or {}
		for _, track in ipairs(tracksToStop) do
			if track.IsPlaying then -- little check
				if track.Animation and track.Animation.AnimationId == animations.Holding.AnimationId then
					track:Stop(0.2) -- stop holding
				end
			end
		end
		currentlyOpenDoorTracks[playerTrain][doorSide] = {} -- clears the stored tracks

		-- plays the sound
		if closeSounds and #closeSounds > 0 then
			for _, s in ipairs(closeSounds) do
				if s then s:Play() end
			end
		end

		-- flashes the lights (this happens in trains in the Netherlands, for warning the people) NOT RELEVANT
		coroutine.wrap(function()
			flashLights(specificDoorLightsTable, animations.Closing.Duration)
		end)()

		-- closing door anim
		local closingTracks = playAndStoreAnimation(animations.Closing)
		task.wait(animations.Closing.Duration) -- wait for finish again

		-- changing weight, but not sure if this is the correct way
		for _, t in ipairs(closingTracks) do
			t:AdjustWeight(1, 1)
		end

		-- not relevant again
		updatePassengerSystemDoors(playerTrain, doorSide, false)

		-- gotta let them know the doors have closed
		if player and trainEvent then
			trainEvent:FireClient(player, "StopDoorFlash", { DoorSide = doorSide })
		else
			warn("someone remove the event")
		end

		printAllDoorStatuses()
	end
end

Would be amazing if someone could help me out👌