Problem with debouncee

I’m trying to get this debounce to work, and although yes it technically does it, it doesn’t do it as soon as it touches the player, just after a few seconds, and sometimes it just doesn’t work at all, i just want it to have a cooldown when it touches a humanoid, any help?

local ball = script.Parent
local folder = workspace.map
local sound = script.Parent.Sound

local db = true

ball.Touched:Connect(function(part)
	if part.Parent.Parent == folder and part.Name == "part" then
		sound:Play()
		part:Destroy()
	end
	
	if db == true then
		db = false
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
			sound:Play()
			humanoid.Jump = true
			print("touched")
			
			wait(3)
			db = true
		end
	end
end)
1 Like

Try this

local ball = script.Parent
local folder = workspace.map
local sound = script.Parent.Sound

local debounce = false

ball.Touched:Connect(function(part)
	if debounce then
		return
	end

	debounce = true

	if part.Parent.Parent == folder and part.Name == "part" then
		sound:Play()
		part:Destroy()
	end

	local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		sound:Play()
		humanoid.Jump = true
		print("touched")
	end

	task.wait(3)
	debounce = false
end)

you dont need to use local when using debounce.

Wdym? Why would I make it a global?

ball.Touched:Connect(function(part)

      ball.CanTouch=false

      --code here
      
      task.wait(3)
      ball.CanTouch=true
end)

same thing, it either activates once or doesn’t do it, eeven if the part is touched, it also messes up the lines to destroy the non humanoid parts

local ball = script.Parent
local folder = workspace.map
local sound = script.Parent.Sound

local db = true

ball.Touched:Connect(function(part)
if (part.Parent==folder or part.Parent.Parent==folder)and part.Name == "part" then
sound:Play()
part:Destroy()
return -- Don't continue when part just destroyed
end

if db == true then
db = false
local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
sound:Play()
humanoid.Jump = true
print("touched")
wait(3)
db = true
end
end
end)

Should even suppose to be?