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

How do i put a debounce on a function?

Asked by 6 years ago

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

Log in to vote
0
Answered by
T0XN 276 Moderation Voter
6 years ago
Edited 6 years ago

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.

0
I've read dozens of tutorials on debounces but only this one made me comprehend. Thank you, it worked perfectly! JamarMario 13 — 6y
Ad

Answer this question