Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Best way to add a debounce to this opening door script?

I'm so bad at debounces, any help?

local door = script.Parent
local main = door.Main
local button = script.Parent.MainDoor

local open = false
button.ClickDetector.MouseClick:Connect(function()
	if not open then
		open = true
		for i = 0, 10, 0.3 do
			door:SetPrimaryPartCFrame(door:GetPrimaryPartCFrame()
			* CFrame.Angles(0, math.rad(math.pi*-0.85), 0))
			wait()
		end
	else
		open = false
		for i = 0, 11, 0.3 do
			door:SetPrimaryPartCFrame(door:GetPrimaryPartCFrame()
			* CFrame.Angles(0, math.rad(math.pi*0.80), 0))
			wait()
		end	
	end
end)


2 answers

✓ Accepted answer

The easiest way to add a debounce is to do the check if the debounce is true after the function starts, immediately set it to false after, and then set it back to true the line before the end for that check.

local door = script.Parent
local main = door.Main
local button = script.Parent.MainDoor
local debounce = true

local open = false
button.ClickDetector.MouseClick:Connect(function()
    if debounce == true then
        debounce = false
        if not open then
            open = true
            for i = 0, 10, 0.3 do
                door:SetPrimaryPartCFrame(door:GetPrimaryPartCFrame()
                * CFrame.Angles(0, math.rad(math.pi*-0.85), 0))
                wait()
            end
        else
            open = false
            for i = 0, 11, 0.3 do
                door:SetPrimaryPartCFrame(door:GetPrimaryPartCFrame()
                * CFrame.Angles(0, math.rad(math.pi*0.80), 0))
                wait()
            end 
        end
        debounce = true
    end
end)

I imagine a boolean would be simple

local door = script.Parent
local main = door.Main
local button = script.Parent.MainDoor

local open = false
local opening = false

button.ClickDetector.MouseClick:Connect(function()
	if opening then return end
	opening = true
	-- Your whole script here
	opening = false
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.