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 add a debounce to this?

When I touch a part, I get anchored. I want to have a debounce so it can wait about 1 second before it could get stepped on again.

script.Parent.Touched:connect(function(part) 
	
if part.Parent:FindFirstChild('Torso') then 
	part.Parent.Torso.Anchored = true 
	wait(15)
	part.Parent.Torso.Anchored = false
	
end 
end)

1 answer

✓ Accepted answer

So a debounce is basically a variable that you set to true or false depending on when you want something to happen. So in your situation, we need to allow the function to work once, and then after it's done wait 1 second, then allow it again.


To do this, we can use any variable, but we'll just call it 'Debounce' for simplicity.

local Debounce = false --Sets the debounce variable

script.Parent.Touched:connect(function(part) 
	if part.Parent:FindFirstChild('Torso') then 
		if not Debounce then --Checks if it's currently allowed to pass
			Debounce = true --Sets to true so it wont happen again
			part.Parent.Torso.Anchored = true 
			wait(15)
			part.Parent.Torso.Anchored = false
			wait(1)
			Debounce = false --Sets it back to false so it's allowed to happen again.
		end 
	end
end)

So now your script should work. If you have any further problems/questions, please leave a comment below, and I'll see what I can do. Hope I helped :P

####-Dyler3

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