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