Hello folks. I am trying to make a garage door for my game, however, whenever I join the game and click to open it for the first time, it does not open. The noise plays as if it were opening, however, the door does not move. After this first click though, the door will open and close like normal. Can someone help me fix this issue?
Here is the script used to make it open/close:
local ClickDetector = script.Parent.ClickDetector
local closePos = script.Parent.Parent.DoorClose
local Door = script.Parent
local openPos = Door.CFrame * CFrame.Angles(0,0,0)
local closed = true
local doorTime = 4.5
local Sound = script.Parent.Sound
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(doorTime,Enum.EasingStyle.Linear,Enum.EasingDirection.Out, 0, false, 0 )
ClickDetector.MouseClick:Connect(function()
if closed then
tweenService:Create(Door,tweenInfo,{CFrame = openPos}):Play()
Sound:Play()
closed = false
else
tweenService:Create(Door,tweenInfo,{CFrame = closePos.CFrame}):Play()
Sound:Play()
closed = true
end
end)
So, it seems like the issue with the garage door not opening on the first click is that it’s not starting in the correct position. To fix this, we need to add a line of code that sets the door to its initial position before the “if closed then…else…” loop in the “ClickDetector.MouseClick” event.
Check this code with the added line:
local ClickDetector = script.Parent.ClickDetector
local closePos = script.Parent.Parent.DoorClose
local Door = script.Parent
local openPos = Door.CFrame * CFrame.Angles(0,0,0)
local closed = true
local doorTime = 4.5
local Sound = script.Parent.Sound
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(doorTime,Enum.EasingStyle.Linear,Enum.EasingDirection.Out, 0, false, 0 )
ClickDetector.MouseClick:Connect(function()
Door.CFrame = closePos.CFrame -- Add this line to set the door to its initial position
if closed then
tweenService:Create(Door,tweenInfo,{CFrame = openPos}):Play()
Sound:Play()
closed = false
else
tweenService:Create(Door,tweenInfo,{CFrame = closePos.CFrame}):Play()
Sound:Play()
closed = true
end
end)
I did what you said and it worked! Thanks! I can’t believe I didn’t think to do this beforehand. I switched the positions in the script and now the door opens on the first click.