How to make door close

Hello guys,

So in my horror game, I want the doors to go back to being closed a few seconds after it being opened. I’m not experienced in scripting to a pro level but I got some working code here that manages to make the door open with tween service, how do I make it go back to its closed state?

local frame = script.Parent
local openSound = frame:WaitForChild(“DoorOpen”)
local closeSound = frame:WaitForChild(“DoorClose”)
local clickDetector = frame:WaitForChild(“ClickDetector”)
local model = frame.Parent
local frameClose = model:WaitForChild(“DoorFrameClose”)
local frameOpen = model:WaitForChild(“DoorFrameOpen”)
local opened = model:WaitForChild(“Opened”)
local tweenService = game:GetService(“TweenService”)
local debounce = true
clickDetector.MouseClick:Connect(function()
if debounce == true then
debounce = false
if opened.Value == true then
opened.Value = false

        closeSound:Play()
        tweenService:Create(frame,TweenInfo.new(0.28),{CFrame = frameClose.CFrame}):Play()
    else
        opened.Value = true
        openSound:Play()
        tweenService:Create(frame,TweenInfo.new(0.28),{CFrame = frameOpen.CFrame}):Play()
    end
   
    wait(.35)
    debounce = false
end

end)

1 Like

Get the CFrame of the closed state and open state and tween between them.
You could wait a few seconds and if the player isn’t near the door, close it again.

How would I be able to do that by adjusting the code that I put? I don’t specialize in scripting, sorry.

I want it to close regardless, even if they’re nearby.

You already have the close and open lines, just put this after it opened:

wait(2)

tweenService:Create(frame,TweenInfo.new(0.28),{CFrame = frameClose.CFrame}):Play()

Btw I would save the tweens in a variable so you don’t have to create them new every time. Using tweenService:Create():Play() is only good when you are playing the tween once.

1 Like

Thanks, it closes after the wait time now. The thing now is that it doesn’t want to open again after it closes. How can I like allow the script to be played over and over infinite times without it disabling after being used once?

Ok, I’ll keep it in mind.

You are using debounce the “wrong” or unusual way. You would have to make debounce true in the 3rd last line.

2 Likes

Thanks!! Got it to work perfectly :slight_smile: