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

How do i put a debounce on a function?

I have this little project of mine and i needed to add a debounce to a function. I've tried for hours but i can't figure it out. Piece of code below.

local part = --reminder to do this

part.Touched:connect(function(hit)
    if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid:TakeDamage(1)
    end
end)

1 answer

✓ Accepted answer

Debounce requires use of a boolean and a wait() statement to determine when the player is allowed to fire an event.

In your case, your script would look something like this:

local part = ...
local debounce = false

part.Touched:Connect(function(hit) -- :connect() is deprecated, use :Connect() instead
    if hit.Parent:FindFirstChild("Humanoid") and not debounce then -- Only fire the event if the debounce is set to false
	debounce = true -- Set the debounce to true so that the player can't fire the event again
        hit.Parent.Humanoid:TakeDamage(1)
	wait(1) -- However long you want to wait before the player can get damaged again
	debounce = false
    end
end)

Hope this answered your question.

1 comment

0

I've read dozens of tutorials on debounces but only this one made me comprehend. Thank you, it worked perfectly!

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