Animation not behaving properly

I want to make a door, but instead of fiddling around with tweens and positions, I thought to use Animations instead (Because I had tried using them before for something other than rigging).

And it works! For the most part…

As you can see above, when I step on the button, it opens the door, when I step off of it, the door closes. But when I step on it again, after the door opens, it shuts again.

The code responsible for the door can be found below:

local ContentProvider = game:GetService("ContentProvider")

local openEvent : BindableEvent = script.Parent.Open
local closeEvent : BindableEvent = script.Parent.Close

local config : Configuration = script.Parent.Parent.Config

function loadAnim(animId)
	local animation = Instance.new("Animation")
	animation.AnimationId = animId
	ContentProvider:PreloadAsync({animation})
	return animation
end

local animator = script.Parent.Humanoid.Animator

local anims = {
	Open = loadAnim("rbxassetid://8089157174"),
	Close = loadAnim("rbxassetid://8089164005"),
	BindOpen = loadAnim("rbxassetid://8089165516"), -- 8089165516
	BindClose = loadAnim("rbxassetid://8089166933") -- 8089166933
}

local lastAnim : AnimationTrack
local lastAnimBindEvent : RBXScriptConnection

local opened = config.OpenedByDefault.Value

function open()
	--if lastAnim then lastAnim:Stop(0.1) end
	if lastAnimBindEvent then lastAnimBindEvent:Disconnect() end
	local animTrack : AnimationTrack = animator:LoadAnimation(anims.Open)
	animTrack:Play(0)
	lastAnimBindEvent = animTrack.Stopped:Connect(function()
		local animTrack : AnimationTrack = animator:LoadAnimation(anims.BindOpen)
		animTrack:Play(0)
	end)
end

function close()
	--if lastAnim then lastAnim:Stop(0.1) end
	if lastAnimBindEvent then lastAnimBindEvent:Disconnect() end
	local animTrack : AnimationTrack = animator:LoadAnimation(anims.Close)
	animTrack:Play(0)
	lastAnimBindEvent = animTrack.Stopped:Connect(function()
		local animTrack : AnimationTrack = animator:LoadAnimation(anims.BindClose)
		--animTrack.Priority = Enum.AnimationPriority.Action
		animTrack:Play(0)
	end)
end

openEvent.Event:Connect(function()
	if not opened then
		opened = true
		open()
	end
end)

closeEvent.Event:Connect(function()
	if opened then
		opened = false
		close()
	end
end)

(BTW, I BindClose/BindOpen is a looping animation of the door opened/closed)

But to clear up some confusion, let me tell what is/isn’t happening:

  1. I tried placing logpoints and have found that close isn’t being called when it isn’t supposed to. It gets called just fine.
  2. It likely has something due to either animation priority or how it is handled. After the BindOpen animation is played in the open function, it gives up and just plays the one found in the close function. If I comment out the second LoadAnimation in close, the one in the open function works just fine! But now the first close animation loops.

So my question is, why does it ignore the BindOpen animation and just go back to the BindClose one?


(If I forgot to mention anything, please let me know so I can provide further information.)

Add a debounce to the function which handles the floor button so that when it is stood on it doesn’t trigger the animation to open and close the door too frequently.

Thanks for your help, I fixed the issue.

Unfortunately, this didn’t have to do with debounce issues, I had already set up the button script to only fire once it is stepped on, or it is unstepped.

However, my fix was just to store the last animation, and cancel it before playing the BindOpen animation.

Final Code:

local ContentProvider = game:GetService("ContentProvider")

local openEvent : BindableEvent = script.Parent.Open
local closeEvent : BindableEvent = script.Parent.Close

local config : Configuration = script.Parent.Parent.Config

function loadAnim(animId)
	local animation = Instance.new("Animation")
	animation.AnimationId = animId
	ContentProvider:PreloadAsync({animation})
	return animation
end

local animator = script.Parent.Humanoid.Animator

local anims = {
	Open = loadAnim("rbxassetid://8089157174"),
	Close = loadAnim("rbxassetid://8089164005"),
	BindOpen = loadAnim("rbxassetid://8089165516"), -- 8089165516
	BindClose = loadAnim("rbxassetid://8089166933") -- 8089166933
}

local lastAnim : AnimationTrack

local opened = config.OpenedByDefault.Value

function open()
	local animTrack : AnimationTrack = animator:LoadAnimation(anims.Open)
	animTrack:Play(0)
	animTrack.Stopped:Connect(function()
		if lastAnim then lastAnim:Stop(0) end
		local animTrack : AnimationTrack = animator:LoadAnimation(anims.BindOpen)
		animTrack:Play(0)
		lastAnim = animTrack
	end)
end

function close()
	local animTrack : AnimationTrack = animator:LoadAnimation(anims.Close)
	animTrack:Play(0)
	animTrack.Stopped:Connect(function()
		if lastAnim then lastAnim:Stop(0) end
		local animTrack : AnimationTrack = animator:LoadAnimation(anims.BindClose)
		animTrack:Play(0)
		lastAnim = animTrack
	end)
end

openEvent.Event:Connect(function()
	if not opened then
		opened = true
		open()
	end
end)

closeEvent.Event:Connect(function()
	if opened then
		opened = false
		close()
	end
end)