Debounce only works once

Hi everyone

I am trying to create a basic script where I touch a part, and 5 clones are created.

The debounce works the first time I touch the part, and the 5 clones are created, but then, after waiting 25 seconds, the debounce seems to be inactive and thousands of clones are created when I touch the part.

local counter = 0
local debounce = false
hitBox.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		if not debounce then
			debounce = true
			repeat
				local Clone = part:Clone()
				Clone.Parent = workspace		
				counter = counter + 1
				print ("Part created planted")
			until counter == 5
			wait(25)
			debounce = false

I think you could try inversing the Humanoid and the debounce if maybe this will work.

your loop stops when the counter is at 5. When you touch the part for a second time, the counter has already passed 5, so its going to continue adding on to the counter infinitely. you could reset the counter each time you touch the part.

I revised it:

local counter = 0
local debounce = false
hitBox.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		if debounce == true then return end
			debounce = true
			repeat
				local Clone = part:Clone()
				Clone.Parent = workspace		
				counter = counter + 1
				print ("Part created planted")
			until counter == 5
			wait(25)
			debounce = false

this should probably work

edit: you can also try @cawin6’s response

1 Like

Perfect, it worked. Thank you. Also thank you for the explanation.

I would of done the same thing by explaining, and remaking the code.
Yet I was too slow to reply with this topic…

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.