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

How do I fix my debounce for a touched event? What am I doing wrong?

The idea of the code is self explanatory. Basically, If I touch a brick I print "hello". Since it prints too many times, I want to add a debounce. However, my debounce isn't doing anything when I step on it, what's wrong?

debounce = false
test = script.Parent

test.Touched:connect(function(x)
	lol = x.Parent:FindFirstChild("Humanoid")

	if lol ~= nil and debounce ~= false then 
	debounce = true 

		if debounce == false then
		print ("Hello")
		debounce = false
	end
end

1 answer

✓ Accepted answer

So first you set debounce = false. Then, when you connect the touched event, you check debounce ~= false. This will prevent the code from running ever. Instead, you need to check if debounce == false then then immediately set debounce = true and after all the code, right before you end the debounce if block, set debounce = false. That should be it.

local debounce = false --it's usually better to make variables local
local test = script.Parent

test.Touched:connect(function(x)
    local lol = x.Parent:FindFirstChild("Humanoid")

    if lol ~= nil and debounce == false then 
    	debounce = true 
        print ("Hello")
        debounce = false
    end
end

3 comments

0

I suggest fixing your spelling error at the first sentence saying 'debound' Also, a local variable doesn't matter if it's at the outermost scope, this changes nothing from having local there and having it not there. lol, however, should be local like it is.

0

Thank you for your help everyone! This worked. For future reference, just don't forget to add "wait ()" after print ("Hello").

0

I tried to incorporate debounce into my real project, but I still feel like I don't understand "debounce" too well. How does it work? How does making a debounce true change the entire code?

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