So someone told me in order to limit input from events I could create a debounce. Can someone explain what a debounce is and give me an example for it?
What is a debounce?
A debounce
is a few lines of code that prevent a certain thing from running too much within a period of time. Normally, these are put in to prevent too many changes from happening at once.
For example:
01 | local debounce = false |
02 | local part = game.Workspace.Part |
03 |
04 | part.Touched:connect( function (hit) |
05 | if not debounce then |
06 | debounce = true |
07 | print ( "I've been touched!" ) |
08 | wait( 5 ) |
09 | debounce = false |
10 | end |
11 | end |
You can change game.Workspace.Part to whatever block you want. And your Touched function doesn't need to be printing something. The normal output would be something like:
I've been touched!
I've been touched!
I've been touched!
I've been touched!
I've been touched!
without any pause in between each print, so it clogs up the console.
But with debounce, it should look something more like this:
I've been touched!
.....
I've been touched!
with a 5 second pause in between each print.
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB8QFjAAahUKEwj12sKwtZ3HAhUGEpIKHYRQBMA&url=http%3A%2F%2Fwiki.roblox.com%2Findex.php%3Ftitle%3DDebounce&ei=ggXIVfWRF4akyASEoZGADA&usg=AFQjCNFQF6tnlOjSI49vTOZpfZhVt5Bxzw&bvm=bv.99804247,d.aWw
roblox wiki over debounces
01 | otherTeleporter = script.Parent.TeleporterB |
02 |
03 | function teleport(hit) |
04 | p = game.Players:GetPlayerFromCharacter(hit.Parent) |
05 | if p then |
06 | if p.Character then |
07 | if p.Character.Humanoid.Health < 0 then |
08 | p.Character.Torso.CFrame = otherTeleport.CFrame + Vector 3. new( 0 , 3.5 , 0 ) |
09 | end |
10 | end |
11 | end |
12 | end |
13 |
14 | script.Parent.Touched:connect(teleport) |
A debouncer is commonly used to prevent functions, or snippets of code, from being run too many times at once. For example, with a two-way teleporter, if there was no debounce, you would be stuck in an unescapable teleport loop.
The snippet above is a working teleporter script. But there's one problem: There's no debouncer! It would cause an infinite teleport loop, and the function would be called much too many times.
We need to fix that with a debouncer.
01 | otherTeleporter = script.Parent.TeleporterB |
02 | debounce = false -- The debouncer |
03 |
04 | function teleport(hit) |
05 | if debounce = = false then -- Make sure debounce is false |
06 | debounce = true -- Set debounce to true |
07 | p = game.Players:GetPlayerFromCharacter(hit.Parent) |
08 | if p then |
09 | if p.Character then |
10 | if p.Character.Humanoid.Health < 0 then |
11 | p.Character.Torso.CFrame = otherTeleport.CFrame + Vector 3. new( 0 , 3.5 , 0 ) |
12 | end |
13 | end |
14 | end |
15 | wait( 2 ) -- Give player time to step off teleporter |
16 | debounce = false -- Allow the teleporter to teleport another player |
17 | end |
18 | end |
19 |
20 | script.Parent.Touched:connect(teleport) |
That's how you use debouncers. Yay! We now have a working teleporter, that will teleport players (and NPC's) anywhere TeleporterB is located. Now all we need to do is make Teleporter B teleport back, but remember the debouncer.
A debounce is a stopper essentially after a certain constraint met. Its usually used by a local value outside the function changed inside the function to stop a loop example
01 | local reached = false |
02 | num = 0 |
03 | want = 20 |
04 | while true do |
05 | wait( 1 ) |
06 | If num ~ = want then |
07 | num = num + 1 |
08 | end |
09 | If num = = want then |
10 | reached = true break |
11 | end |
12 | end |