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 8 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 — 8y
0
Dawn, you didn't describe a debounce, you described stopping a function once a condition has been met YellowoTide 1992 — 8y

4 answers

Log in to vote
3
Answered by
yelsew 205 Moderation Voter
8 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:

local debounce = false
local part = game.Workspace.Part

part.Touched:connect(function(hit)
    if not debounce then
        debounce = true
        print("I've been touched!")
        wait(5)
        debounce = false
    end
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.

Ad
Log in to vote
0
Answered by 8 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 8 years ago

otherTeleporter = script.Parent.TeleporterB function teleport(hit) p = game.Players:GetPlayerFromCharacter(hit.Parent) if p then if p.Character then if p.Character.Humanoid.Health < 0 then p.Character.Torso.CFrame = otherTeleport.CFrame + Vector3.new(0,3.5,0) end end end end 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.


otherTeleporter = script.Parent.TeleporterB debounce = false -- The debouncer function teleport(hit) if debounce == false then -- Make sure debounce is false debounce = true -- Set debounce to true p = game.Players:GetPlayerFromCharacter(hit.Parent) if p then if p.Character then if p.Character.Humanoid.Health < 0 then p.Character.Torso.CFrame = otherTeleport.CFrame + Vector3.new(0,3.5,0) end end end wait(2) -- Give player time to step off teleporter debounce = false -- Allow the teleporter to teleport another player end end 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.

Log in to vote
-1
Answered by 8 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

local reached = false
num = 0
want = 20
while true do
wait(1)
If num ~= want then
num = num +1
end
If num == want then
reached = true break
end
end

Answer this question