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

How to apply debounce to this door?

Asked by 3 years ago

I have successfully made a door using tweenservice. The problem is that I do not know how to prevent users from spamming the door open and closed. I would like to know how exactly would I make it so there is a delay and the user is not able to spam the door. So far, the debounce boolean I made does not work. Script:

local Door = script.Parent.Door
local debounce = false
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local DoorOpen = {CFrame = Door.CFrame + Door.CFrame.lookVector * 6}
local DoorClose = {CFrame = Door.CFrame }
local open = TS:Create(Door, TI, DoorOpen)
local close = TS:Create(Door, TI, DoorClose)
local sound = script.Parent["BMRF Sliding Door Open"]
local closesound = script.Parent["BMRF Sliding Door Close"]

script.Parent.ToggleDoor.OnServerEvent:Connect(function()
    if debounce == false then
        debounce = true
        sound:Play()
        open:Play()
    elseif debounce == true then
        debounce = false
        closesound:Play()
        close:Play()
    end
end)

3 answers

Log in to vote
1
Answered by 3 years ago

I have no idea if this is what you are wanting to add, but from what I understood you wanted the door to have a cooldown so you couldn't spam it.

I am not 100% sure if this script will work but I wanted to give answering a try.

local Door = script.Parent.Door
local debounce = false
local cooldown= false
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local DoorOpen = {CFrame = Door.CFrame + Door.CFrame.lookVector * 6}
local DoorClose = {CFrame = Door.CFrame }
local open = TS:Create(Door, TI, DoorOpen)
local close = TS:Create(Door, TI, DoorClose)
local sound = script.Parent["BMRF Sliding Door Open"]
local closesound = script.Parent["BMRF Sliding Door Close"]

script.Parent.ToggleDoor.OnServerEvent:Connect(function()
    if cooldown == false then
        cooldown = true
        if debounce == false then
            debounce = true
            sound:Play()
            open:Play()
        elseif debounce == true then
            debounce = false
            closesound:Play()
            close:Play()
        end
        wait(1)
        cooldown = false
    end
end)

Cheers, Jack

0
Your right! But just a note, if you want a better wait, you can yield until the sound and the open sound ends. Xapelize 2658 — 3y
Ad
Log in to vote
0
Answered by
pwx 1581 Moderation Voter
3 years ago

Well knowing that your tween is 1 second, all you'd have to do is wait the 1 second or use Tween.Completed().

local tweenService = game:GetService('TweenService')

local Door = script.Parent.Door
local debounce = false
local isOpen = false

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

local doorOpen = {
    CFrame = Door.CFrame + Door.CFrame.lookVector * 6
}
local doorClose = {
    CFrame = Door.CFrame
}

local Open = tweenService:Create(Door, tweenInfo, doorOpen)
local Close = tweenService:Create(Door, tweenInfo, doorClose)

local openSound = script.Parent["BMRF Sliding Door Open"]
local closeSound = script.Parent["BMRF Sliding Door Close"]

script.Parent.ToggleDoor.OnServerEvent:Connect(function(Player)
    if debounce == true return end
    debounce = true
    if isOpen == false then
        openSound:Play()
        Open:Play()
        Open.Completed:Connect(function()
            debounce = false
        end) -- completed
    else
        closeSound:Play()
        Close:Play()
        Close.Completed:Connect(function()
            debounce = false
        end) -- completed
    end -- check if closed/open
    isOpen = not isOpen -- toggle between true/false lol
end) -- onServerEvent


Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Your debounce is instantly closed and opened. You can consider inserting a wait(1) between the open/ closing door function.

Example:

debounce = false

function Test()
    debounce = true
    print("test")
    debounce = false
end

if debounce = false then
script.Parent.Touched:Connect(Test)
end

This will not work, as it spams the "test" print. Instead, you add a wait(1) between the function,

debounce = false

function Test()
    debounce = true
    wait(1)
    print("test")
    debounce = false
end

if debounce = false then
script.Parent.Touched:Connect(Test)
end

It only fires once every 1 second when you are touching it.

Answer this question