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:
- I tried placing logpoints and have found that close isn’t being called when it isn’t supposed to. It gets called just fine.
- 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 theclose
function. If I comment out the second LoadAnimation inclose
, the one in theopen
function works just fine! But now the firstclose
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.)