Events
Every time an event fires, the code inside the function it's associated with (also known as it's callback) runs as it's own individual task. It would essentially be the same as spawning a function, or taking advantage of the coroutine library. This just means when you call a yielded function, it will only effect that task while the others take their turn to execute. This is because coroutines actually take turns running instead of running simultaneously, but that's for a different discussion.
Debounce
Debounce is the solution you're really looking for. Debounce acts as a gate between what can and can't happen, and is typically controlled using boolean values (true and false) with an if statement to check their conditions. It's important to toggle this value, however. If you leave your debounce in a single state, the body of your code will only run once (assuming you've switched it at least once).
The entire point of using a debounce is to give it a default value (in this case we're going to assign it true by default), then evaluate it while also inverting it (so the condition will differ next time around).
Implementation
The description I gave above may seem convoluted, but it's really simple. It's just a yes/no condition in a program to execute a body of instructions. Here's an example:
local debounce = true
local function sample()
if debounce == true then
debounce = false
print("This code has ran!")
else
debounce = true
print("Waiting to run code")
end
end
sample()
sample()
sample()
If you run the code above, you'll see you get these results:
This code has ran!
Waiting to run code
This code has ran!
However, there is a shorter way we could word this. Most of the time, people only care about the part of the debounce that allows the code to run, and don't care about an alternative set of instructions. To implement a shorter version, we could simply check if debounce is false, and if it is then use the return statement to discontinue the execution of the body it's a descendant of. We'll still have to toggle the value, however. Example:
local debounce = true
local function sample()
if not debounce then
return
end
debounce = false
print("The debounce must be true if you see this!")
wait(1)
debounce = true
end
sample()
sample()
wait(1.5)
sample()
Needless to say, there's a million ways you can implement debounce, so I'm not going to cover any more examples. Furthermore, here would be your revised code using debounce to wait before firing the event more than once at a time:
local debounce = true
script.Parent.Touched:connect(function(hit)
if debounce == false then
return
end
debounce = false
local human = hit.Parent:FindFirstChild("Humanoid")
local damage = script.Parent.Parent.EnemySettings.Damage.Value
if human then
wait(2)
human.Health = human.Health - damage
end
debounce = true
end)
Hope this helped, let me know if you have any questions.
3 comments
Thanks so much and as a bonus i now know how to use debounce! my only question is that actually something that roblox made or did developers just decide to use it?
Why are you using
elseifwhen you meanelse?Not sure, thanks for pointing that out. Corrected it.