How do I make the door close after a while?

Ok, this is my last time asking for help for a while, but, I am still confused about how to make it so that the door closes after like a couple of seconds. Can someone help again?

local amountOfTimes = 35
function onClicked()
for count = 1, amountOfTimes do
	script.Parent.Parent.Part.Position = script.Parent.Parent.Part.Position - Vector3.new(0.1, 0, 0)
	wait(0.01)
end
end
function close()
for count = 1, amountOfTimes do
	script.Parent.Parent.Part.Position = script.Parent.Parent.Part.Position + Vector3.new(0.1, 0, 0)
	wait(0.01)
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
wait(2)
script.Parent.ClickDetector.MouseClick:connect
2 Likes

You would just have to call the close() function after two seconds, like so.

script.Parent.ClickDetector.MouseClick:connect(function()
    onClicked()
    wait(2)
    close()
end)

Additionally, if you want to prevent the player from firing the MouseClick event when it’s in the process of opening and closing, you would have to add a debounce.

3 Likes

Ok thanks, but for real though this is my last question, sorry for asking so many questions. How do I make it so players can’t keep spam pressing the door so it keeps on moving?

As I said, add a debounce, like so.

local debounce = false

script.Parent.ClickDetector.MouseClick:connect(function()
    if not debounce then
        debounce = true
        onClicked()
        wait(2)
        close()
        debounce = false --The player can start clicking it again when this is written
    end
end)
4 Likes

Ok, thank you so much for your help!

1 Like