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

Hi, How do I add a Debounce to my ClickDetector script?

Asked by 4 years ago

I'm trying to make a Button Door and I don't want people spamming the button and playing the sound and opening and closing the door a million times, how can I fix that?

open = false

door1 = script.Parent.Parent.Parent.Door1

doorpart1 = script.Parent.Parent.Parent.DoorPart1

door2 = script.Parent.Parent.Parent.Door2

doorpart2 = script.Parent.Parent.Parent.DoorPart2


local TweenService = game:GetService("TweenService")

local part = script.Parent



local tweenInfo = TweenInfo.new(6,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)



local Door1Open = {CFrame = door1.CFrame + door1.CFrame.lookVector * -7}

local Door2Open = {CFrame = door2.CFrame + door2.CFrame.lookVector * 7}

local Door1Close = {CFrame = door1.CFrame }

local Door2Close = {CFrame = door2.CFrame }

local DoorPart1Open = {CFrame = doorpart1.CFrame + doorpart1.CFrame.LookVector * -7}

local DoorPart2Open = {CFrame = doorpart2.CFrame + doorpart2.CFrame.LookVector * 7}

local DoorPart1Close = {CFrame = doorpart1.CFrame}

local DoorPart2Close = {CFrame = doorpart2.CFrame}


local Open1 = TweenService:Create(door1,tweenInfo,Door1Open)

local Open2 = TweenService:Create(door2,tweenInfo,Door2Open)

local Close1 = TweenService:Create(door1,tweenInfo,Door1Close)

local Close2 = TweenService:Create(door2,tweenInfo,Door2Close)

local OpenDoorPart1 = TweenService:Create(doorpart1,tweenInfo,DoorPart1Open)

local OpenDoorPart2 = TweenService:Create(doorpart2,tweenInfo,DoorPart2Open)

local CloseDoorPart2 = TweenService:Create(doorpart2,tweenInfo,DoorPart2Close)

local CloseDoorPart1 = TweenService:Create(doorpart1,tweenInfo,DoorPart1Close)

script.Parent.MouseClick:connect(function()

if open == false then

Open1:Play()

Open2:Play()

OpenDoorPart1:Play()

OpenDoorPart2:Play()

script.Parent.BlackMesaHeavyDoorOpen:Play() 


    wait(2)

open = true

else

Close1:Play()

Close2:Play()

CloseDoorPart1:Play()

CloseDoorPart2:Play()

script.Parent.BlackMesaHeavyDoorClose:Play()

wait(2)

open = false

end

end)

1 answer

Log in to vote
0
Answered by 4 years ago

You can use a locking flag. You'd first declare it at the top like this:

local busy = false

Then, your click function would use it like this:

script.Parent.MouseClick:Connect(function()

    if busy then return end -- Ignores clicks while door is busy opening or closing

    busy = true

    if not open then
        Open1:Play()
        Open2:Play()
        OpenDoorPart1:Play()
        OpenDoorPart2:Play()
        script.Parent.BlackMesaHeavyDoorOpen:Play() 
    else
        Close1:Play()
        Close2:Play()
        CloseDoorPart1:Play()
        CloseDoorPart2:Play()
        script.Parent.BlackMesaHeavyDoorClose:Play()
    end

    wait(2)
    open = not open

    busy = false

end)
0
Thanks. DesiredToResign 10 — 4y
Ad

Answer this question