Click to Open Animation script not working

Hi all,
I am making a click to open door with an animation, but I can not get it to work for the life of me. If anybody can peek at the script and make sure I didn’t miss anything, much appreciated!

local door = script.Parent
local clickDetector = door:FindFirstChild(“ClickDetector”)

local animation = Instance.new(“Animation”)
animation.AnimationId = “rbxassetid://124734189622427” 

local animator = door:FindFirstChildOfClass(“AnimationController”):FindFirstChildOfClass(“Animator”)

local isOpen = false 

clickDetector.MouseClick:Connect(function()
if animator then
if isOpen then

local closeAnim = animator:LoadAnimation(animation)
closeAnim:Play()
closeAnim.Stopped:Wait() 
isOpen = false
else

local openAnim = animator:LoadAnimation(animation)
openAnim:Play()
openAnim.Stopped:Wait() 
isOpen = true
end
else
warn(“Animator Not Found During Click”)
end
end)

If i have understood correctly then you should be using the tweenservice instead of an animation. Have the visible door model, model2, and model3. Model 2 will be where the door needs to end up when its open and model3 where it should be when its closed. You then detect whether the door is open and play the according tween from there!

Animation is NOT a physics modifier. In other words, you shouldn’t be using animations to perform physics operations like opening and closing doors. What you should be doing instead is using “TweenService”, a service that modifies the physical properties of instances smoothly. Here’s how to implement it

local TS = game:GetService(“TweenService”)
local door = script.Parent — Assuming door is a model 

local TI = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

door.ClickDetector.MouseClick:Connect(function()
   if IsOpen then
      local goal = {CFrame *= CFrame.Angles(0,math.rad(-90),0)}
      local tween = TS:Create(door.PrimaryPart, TI, goal)
      tween:Play()
      tween.Completed:Wait()
      IsOpen = false
   else
       local goal = {CFrame *= CFrame.Angles(0,math.rad(90),0)}
      local tween = TS:Create(door.PrimaryPart, TI, goal)
      tween:Play()
      tween.Completed:Wait()
      IsOpen = true
   end
end)

If you want to read up on tween service, I’ll leave the link here

1 Like