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

How to apply debounce to this door?

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)

2 answers

I think I got it. You forgot to add a wait in the script. Right after it's disable it gets enabled straight after. And setting the debounce to true should be right after. I can fix it.

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()
        wait(1)
	debounce = false
       closesound:Play()
        close:Play()
        
    end
end)

The debounce you are using is just to see if a door is opened or closed, and it does not prevent it from spamming,,,,, it would be better to use another variable like "if open == false" and then check the debounce

For example;

local Door = script.Parent.Door
local debounce = false
local open = false -- new variable
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 open == false then -- is it OPEN...?
        if debounce == false then -- then check debounce
              debounce = true
              open = true
              sound:Play()
              open:Play()
              wait(1) -- wait a bit (optional)
              debounce = false -- reset the debounce
        end
    elseif open == true then
        if debounce == false then -- same thing as above
              debounce = true
              open = false
              closesound:Play()
              close:Play()
              wait(1)
              debounce = false
        end
    end
end)

Hope this helps

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