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

What is a debounce?

Asked by 9 years ago

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?

1
How did yelsew get the correct answer i answered first and mines more elaborated. User#5978 25 — 9y
0
Dawn, you didn't describe a debounce, you described stopping a function once a condition has been met YellowoTide 1992 — 9y

4 answers

Log in to vote
3
Answered by
yelsew 205 Moderation Voter
9 years ago

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:

01local debounce = false
02local part = game.Workspace.Part
03 
04part.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
11end

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.

Ad
Log in to vote
0
Answered by 9 years ago

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

Log in to vote
0
Answered by 9 years ago
01otherTeleporter = script.Parent.TeleporterB
02 
03function 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 + Vector3.new(0,3.5,0)
09            end
10        end
11    end
12end
13 
14script.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.

01otherTeleporter = script.Parent.TeleporterB
02debounce = false -- The debouncer
03 
04function 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 + Vector3.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
18end
19 
20script.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.

Log in to vote
-1
Answered by 9 years ago

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

01local reached = false
02num = 0
03want = 20
04while true do
05wait(1)
06If num ~= want then
07num = num +1
08end
09If num == want then
10reached = true break
11end
12end

Answer this question