Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

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)

Ad
Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
6 years ago

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)

Answer this question