Animations glitch

Hi everyone, I’m hoping to get help with something I’ve been scratching my head over for a while. I have a door system which uses animations for doors (I’ve done this because I intend to have complex doors that couldn’t just rely on motors or tweens). Here is my issue: the first time you use the close animation it glitches. However, every time after that it is absolutely fine. However, it runs the same code so I’m quite confused.

I tried to fix this by using track.KeyframeReached for the close anim so it triggered when once it was closed but it caused other issues.


First time using door (not ok)

The rest of the time (absolutely fine)

Here is the code that runs every time the door is toggled:

function DoorController:ToggleDoor(door)
	local animController = door:FindFirstChildOfClass("AnimationController")
	if door:GetAttribute("State") == true then -- This bit runs when opening
		local OpenAnim = door.OpenAnim
		local OpenSound = door.OpenSound
		local track = animController:LoadAnimation(OpenAnim)
		local StaticAnim = door.StaticAnim
		local staticTrack = animController:LoadAnimation(StaticAnim)
		doorAnimations[door] = staticTrack
		track:Play()
		OpenSound:Play()
		track.KeyframeReached:Connect(function(name)
			if name == "Open" then
				staticTrack:Play()
				track:Destroy()
			end
		end)
	else -- This bit runs when closing
		local CloseAnim = door.CloseAnim
		local CloseSound = door.CloseSound
		local track = animController:LoadAnimation(CloseAnim)
		track:Play()
		CloseSound:Play()
		doorAnimations[door]:Stop()
		doorAnimations[door]:Destroy()
		doorAnimations[door] = nil
	end
end

Maybe I’m just being stupid but all help is appreciated :slight_smile:

1 Like

Maybe the “State” of the door isn’t changing between true and false. I would recommend using a boolean value instead if this is the problem, and you should also check to make sure that you change the state to false in the door opening part of the script.

I’ve checked that the state of the door does change - as I mentioned above it works after the first time but uses the same code.

When does the animation load into the door? On the first time used? If so, try moving the load section of the animation to the beginning of the script, after waiting for the controller to load, then save that into a variable and call that variable to play the animation? Since it’s on the first time it’s called, maybe the issue has to do with loading and running immediately?

1 Like

Spot on! Thank you very much for your help. In hindsight I should’ve realised I could just load the track once and re-use it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.