Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone show me debounce?

Can you put debounce in my touch script and explain it a little. Thanks.

function onTouch()
	
	print"Hit"
	local ex=Instance.new("Explosion",script.Parent)
	ex.Position=script.Parent.Position
	ex.BlastPressure=10
	ex.BlastRadius=10
	
	
end

script.Parent.Touched:connect(onTouch)

3 answers

✓ Accepted answer

Debounce is a way to prevent the script from running multiple times. The variable could be any name, and practically have any value. Whether it's a number, string, or boolean, debounce will still work! A majority of scripters like to have it as a variable set within the script, with a boolean, rather than having the object as a descendant. So, let's get started!

on = false -- Set the variable for the debounce

script.Parent.Touched:connect(function(hit)
	if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then -- Checks if it is a player
		if on == false then -- checks the value of the debounce
			on = true -- changes it to true
			print("I'm hit!")
			wait(2)
			on = false
		end
	end
end)

I noticed you were wondering if the variable needed to be local. There are 2 types of variables. Global and local. Local variables' values are only accessible within that function. Global variables are accessible throughout the script. The only time it needs to be local, is if it's originally set/defined within a function. Example:

function Test()
	local abc = 123 -- Local Variable
end

Testing = 515 -- Global variable

If you're not originally defining the variable within a function, there's no need for the local tag. Now, with all of that knowledge, apply that to your script!

If you have any questions, feel free to ask in the comments!

4 comments

0

You should generally use local variable whenever possible, even outside of functions. Not only are they more readable, but they will run faster and more efficiently.

0

Yes, I've heard that before...

0

Why would you check if hit has a Parent? That makes absolutely no sense-it will always have a parent.

0

Wow, looks like we have a hater. I suppose you're right. I just tend to have it there, If I ever have a wait before it. In case it is nil

local db = false -- create the variable debounce.
function onTouch()
if db == false then -- check if it's false. When 2 equal signs are used, it will check if db is actually false. when 1 equal sign is used, it will change the value to false.
    db = true -- make it true so it cannot be hit again while carrying out the function.
    print"Hit"
    local ex=Instance.new("Explosion",script.Parent)
    ex.Position=script.Parent.Position
    ex.BlastPressure=10
    ex.BlastRadius=10
    wait(1) -- a db is kinda useless without a wait..
   db = false -- change the debounce back to false so it can be used again.
end
end

script.Parent.Touched:connect(onTouch)

A debounce system is a set of code that keeps a function from running too many times. It comes from the idea of mechanical switch bounce, where a switch bounces when pushed, creating multiple signals. In the context of ROBLOX, this problem occurs mainly with the Touched event, when a part touches another multiple times in a short space of time, but may be useful in other cases as well.

This is how a basic debounce system works:

When an action happens, such as someone pressing your floor button, the script locks any new function calls until a time passes or the action is complete.

8 comments

0

Do i have to put local in front of db?

0

In line 3 why did you put false then after the == since your variable already says db= false, so wouldn't there be no need to put that?

0

Yes. because it is being used outside of the function. in all other places you don't have to. If it works, please mark it as an answer.

0

when we use 2 equal signs, it will check if it is false.

0

I think he's asking why you're checking if it's false. The script checks if db is false so that the function won't run again while db is true, effectively cancelling any other calls to the function until db is false again, which is the beauty of using debounce.

0

Basically in line 3 you're saying 'if false == false then'

0

If the button is pushed again while carrying out the function, then the debounce will equal true. This basically means that when the function is being carried out, the button cannot be pressed. while the function is being carried out, db will equal true due to line 4.

0

@Hungryjaffer You need "local" if it's WITHIN the function, not outside.

I found this way much easier than how you guys explained it...

db=true

function onTouch()
	if db then
		db=false
	
	print"Hit"
	local ex=Instance.new("Explosion",script.Parent)
	ex.Position=script.Parent.Position
	ex.BlastPressure=10
	ex.BlastRadius=10
	wait(1)
	db=true
end
	end
script.Parent.Touched:connect(onTouch)

1 comment

2

I'm glad you understand it! If you want to go more in-depth with it, read my answer!

This discussion is preserved as it was. New answers, comments, and votes are closed.