How do I make my garage door close automatically after 5 seconds?

Hello ! I would like my garage door to close automatically after 5 seconds of the player opening the door and to be able to open it again using “proximityprompt”
therefore the player would no longer be able to close the door but just open it.

local frame = script.Parent
local Frame = game.Workspace.DoorGarage.DoorFrame
local openSound = frame:WaitForChild("DoorOpen")
local closeSound = frame:WaitForChild("DoorClose")
local proximityprompt = Frame:WaitForChild("ProximityPrompt")
local model = frame.Parent
local frameClose = model:WaitForChild("Door-Close")
local frameOpen = model:WaitForChild("Door-Open")
local opened = model:WaitForChild("Opened")
local tweenService = game:GetService("TweenService")

local debounce = true
proximityprompt.Triggered:Connect(function()
	if debounce == true then
		debounce = false
		if opened.Value == true then
			opened.Value = false

			closeSound:Play()
			tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameClose.CFrame}):Play()
		else
			opened.Value = true

			openSound:Play()
			tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameOpen.CFrame}):Play()
		end

		wait(.35)
		debounce = true
	end
end)
16 Likes

Use task.delay.

task.delay(5, door_close_function )

Also upgrade to task.wait instead of wait.

4 Likes

I did not understand I am a beginner, I have to modify On which line?

4 Likes

Create a function above the proximity prompt trigger function.

function CloseDoor()
... Check if its already open ...
....
end
proximityprompt.Triggered:Connect(

Then, inside the proximity prompt triggered function, where you set the opened value to true, call task.delay(5,CloseDoor)

4 Likes

ok thanks but it doesn’t work.

3 Likes
function CloseDoor()
	if (opened.Value == true) then
		opened.Value = false
		tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameClose.CFrame}):Play()
		closeSound:Play()
	end
end
function OpenDoor()
	if (opened.Value == false) then
		opened.Value = true
		task.delay(5,CloseDoor)
		tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameOpen.CFrame}):Play()
		openSound:Play()
	end
end
local debounce = true
proximityprompt.Triggered:Connect(function()
	if debounce == true then
		debounce = false
		if opened.Value == true then
			CloseDoor()
		else
			OpenDoor()
		end
		task.wait(.35)
		debounce = true
	end
end)
6 Likes

This time nothing works it doesn’t even open anymore… :roll_eyes:

3 Likes

Did you paste it in below the local variables?

3 Likes

Add this to the bottom of your code.

opened.GetPropertyChangedSignal("Value"):Connect(function()
	wait(5)
	if opened == true then
		-- write code to close it here
	end
end
3 Likes

You aren’t changing opened’s value here, and you’re opening the door if it’s open.

if opened.Value == true then
	CloseDoor()
	opened = false
else
	OpenDoor()
	opened = true
end
3 Likes

It’s being changed in the function.

3 Likes

My bad. But you still did flip-flop CloseDoor() and OpenDoor().

3 Likes

Oops, editing, my bad, didn’t notice

4 Likes

A Good way would be to calculate the time since an event happened, then fire the event when the condition is met, it doesnt require a separate thread or yielding to do so.

3 Likes

I succeeded, but you have to press E twice to be able to open again… any idea?

local frame = script.Parent
local Frame = game.Workspace.DoorGarage.DoorFrame
local openSound = frame:WaitForChild("DoorOpen")
local closeSound = frame:WaitForChild("DoorClose")
local proximityprompt = Frame:WaitForChild("ProximityPrompt")
local model = frame.Parent
local frameClose = model:WaitForChild("Door-Close")
local frameOpen = model:WaitForChild("Door-Open")
local opened = model:WaitForChild("Opened")
local tweenService = game:GetService("TweenService")

local debounce = true
proximityprompt.Triggered:Connect(function()
	if debounce == true then
		debounce = false
		if opened.Value == true then
			opened.Value = false
		else
			opened.Value = true

			openSound:Play()
			tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameOpen.CFrame}):Play()
			
			wait(2)
			closeSound:Play()
			tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameClose.CFrame}):Play()
			
		end

		wait(.35)
		debounce = true
	end
end)
2 Likes

My edited code should work, have you tried it?

2 Likes

Oh I hadn’t seen it, it works! thanks a lot for your help !!

2 Likes

You’re welcome. Glad I could help

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.