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

How to add a debounce into this button?

local Button = game.Workspace.Dropper.Button

local DB = true

if DB == true then DB = false Button.ClickDetector.MouseClick:Connect(function() local PartDrop = Instance.new('Part', game.Workspace.PartDrops) PartDrop.Position = Vector3.new(0.775, 7.3, 16.345) PartDrop.Size = Vector3.new(1.67, 0.94, 0.97) PartDrop.Name = 'PartDrop' PartDrop.Anchored = false PartDrop.CanCollide = true PartDrop.Transparency = 0 DB = true end) end

The debounce won't work?

2 answers

✓ Accepted answer

Button Events and all events work when they are. The if statement doesn't stop it from firing if you put the if in the button event it will work. I did some code optimization, the anchored can collide, transparency is all set to these properties already so no need to write them.

local Button = game.Workspace.Dropper.Button

local DB = true



 Button.ClickDetector.MouseClick:Connect(function() 
if DB then
DB = false
local PartDrop = Instance.new('Part', game.Workspace.PartDrops) 
PartDrop.Position = Vector3.new(0.775, 7.3, 16.345) 
PartDrop.Size = Vector3.new(1.67, 0.94, 0.97) 
PartDrop.Name = 'PartDrop'
wait() -- put a cooldown here if you like
DB = true 
end
end) 

Okay first things first When you put code wrap it in code blocks like this press that lua icon and you should see bars going like 17 swiggly lines 2 times

put your code inside of there

Anyways

local Button = game.Workspace.Dropper.Button

local DB = false --make sure it is false or the function wont run
Button.ClickDetector.MouseClick:Connect(function() 
if DB then --try not to make it too long (or if you want i just prefer making it short)
return nil --returns nil so it stops the whole function, if you return something basically the end of the function so its  like a error in your script or if you wrap it in a pcall then a error then that dont count (if its in middle of a function then it just returns the value while doing so)
DB = true --makes sure they cant press it again
endlocal PartDrop = Instance.new('Part', game.Workspace.PartDrops) PartDrop.Position = Vector3.new(0.775, 7.3, 16.345) PartDrop.Size = Vector3.new(1.67, 0.94, 0.97) PartDrop.Name = 'PartDrop' PartDrop.Anchored = false PartDrop.CanCollide = true PartDrop.Transparency = 0
DB = false --i do NOT reccomend not adding a wait cause then it would be like if it didn't have debounce

Then you should be fine If this helps make sure to mark it as a solution! Sorry if it was a bit hard to read or convulted.

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