Hi. I’m currently making a kitchen cabinet for my game. What I’ve done is simple: you open and close the cabinet by using TweenService. After testing, however, the cabinet door won’t open up again after being closed. I’m not sure if this is a script issue or its something involved with the parts in the model.
Here is my code followed by a video and an image of the explorer tab:
-- Get services
local TweenService = game:GetService("TweenService")
-- Get instances
local clickDetector = script.Parent
local hinge = script.Parent.Parent.Parent.Hinge
local hingeClosed = script.Parent.Parent.Parent.LeftDoorHingeClosed
local hingeOpen = script.Parent.Parent.Parent.LeftDoorHingeOpen
local hingeClosedCFrame = hingeClosed.CFrame
local hingeOpenCFrame = hingeOpen.CFrame
-- Get toggle value
local isOpen = false
-- Check if animation is playing
local isTweenPlaying = false
-- Set tweens
local hingeTweenOpen = TweenService:Create(hinge, TweenInfo.new(0.65, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {CFrame = hingeOpenCFrame})
local hingeTweenClose = TweenService:Create(hinge, TweenInfo.new(0.45, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {CFrame = hingeClosedCFrame})
-- Get sounds
local open1 = clickDetector.Parent.Open1
local open2 = clickDetector.Parent.Open2
local close = clickDetector.Parent.Close
-- Function to toggle the door upon being clicked
function toggleDoor()
if isTweenPlaying == false and isOpen == false then
local randomNumber = math.random(1, 2)
if randomNumber == 1 then
open1:Play()
else
open2:Play()
end
end
if isOpen == false and isTweenPlaying == false then
isOpen = true
hingeTweenOpen:Play()
isTweenPlaying = true
repeat
task.wait()
until hingeTweenOpen.Completed:Wait()
isTweenPlaying = false
elseif isOpen == true and isTweenPlaying == false then
isOpen = false
hingeTweenClose:Play()
isTweenPlaying = true
task.wait(0.55)
close:Play()
repeat
task.wait()
until hingeTweenClose.Completed:Wait()
isTweenPlaying = false
end
end
clickDetector.MouseClick:Connect(toggleDoor)
I think the issue might be with the cframe values using Completed:Wait() would be better as well If the Issue isn’t solved could you provide me the debugging?
local TweenService = game:GetService("TweenService")
local clickDetector = script.Parent
local hinge = script.Parent.Parent.Parent.Hinge
local hingeClosed = script.Parent.Parent.Parent.LeftDoorHingeClosed
local hingeOpen = script.Parent.Parent.Parent.LeftDoorHingeOpen
local hingeClosedCFrame = hingeClosed.CFrame
local hingeOpenCFrame = hingeOpen.CFrame
local isOpen = false
local isTweenPlaying = false
local hingeTweenOpen = TweenService:Create(hinge, TweenInfo.new(0.65, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {CFrame = hingeOpenCFrame})
local hingeTweenClose = TweenService:Create(hinge, TweenInfo.new(0.45, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {CFrame = hingeClosedCFrame})
local open1 = clickDetector.Parent.Open1
local open2 = clickDetector.Parent.Open2
local close = clickDetector.Parent.Close
local function playOpenSound()
if math.random(1, 2) == 1 then
open1:Play()
else
open2:Play()
end
end
local function toggleDoor()
if isTweenPlaying then return end
isTweenPlaying = true
if not isOpen then
playOpenSound()
isOpen = true
hingeTweenOpen:Play()
hingeTweenOpen.Completed:Wait()
else
isOpen = false
hingeTweenClose:Play()
task.wait(0.55)
close:Play()
hingeTweenClose.Completed:Wait()
end
isTweenPlaying = false
end
clickDetector.MouseClick:Connect(toggleDoor)
-- if it works remove this debugging
print("Hinge Closed CFrame:", hingeClosedCFrame)
print("Hinge Open CFrame:", hingeOpenCFrame)
The main issue was likely related to how to cframe for the open position was being calculated we can give it a try to open position based on a 90 degree rotation from the closed position
local TweenService = game:GetService("TweenService")
local clickDetector = script.Parent
local hinge = script.Parent.Parent.Parent.Hinge
local hingeClosed = script.Parent.Parent.Parent.LeftDoorHingeClosed
local hingeOpen = script.Parent.Parent.Parent.LeftDoorHingeOpen
local hingeClosedCFrame = hinge.CFrame
local hingeOpenCFrame = hingeClosedCFrame * CFrame.Angles(0, math.rad(-90), 0)
local isOpen = false
local isTweenPlaying = false
local hingeTweenOpen = TweenService:Create(hinge, TweenInfo.new(0.65, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {CFrame = hingeOpenCFrame})
local hingeTweenClose = TweenService:Create(hinge, TweenInfo.new(0.45, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {CFrame = hingeClosedCFrame})
local open1 = clickDetector.Parent.Open1
local open2 = clickDetector.Parent.Open2
local close = clickDetector.Parent.Close
local function playOpenSound()
if math.random(1, 2) == 1 then
open1:Play()
else
open2:Play()
end
end
local function toggleDoor()
if isTweenPlaying then return end
isTweenPlaying = true
if not isOpen then
playOpenSound()
isOpen = true
hingeTweenOpen:Play()
else
isOpen = false
hingeTweenClose:Play()
task.wait(0.55)
close:Play()
end
task.wait(0.65)
isTweenPlaying = false
end
clickDetector.MouseClick:Connect(toggleDoor)
print("Hinge Closed CFrame:", hingeClosedCFrame)
print("Hinge Open CFrame:", hingeOpenCFrame)
If the problem still there check the door make sure there are no collisions preventing movement we could try different approch if that not work let me know