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:
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.
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
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.
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