How can I put a debounce here?

Hello! So I am currently trying to make a tool that breaks boxes, similar to a crowbar, and everything works, but on my box script the sound plays twice when it’s hit. I’m guessing I might need a debounce, but I’m just not sure how to implement it.
Here is my code:

local Box = script.Parent
local Sound = Box:WaitForChild("Sound")

Box.Touched:Connect(function(hit)
	if hit.ClassName == "Part" and hit.Name == "Handle" then
		Box.Transparency = 1
		Box.CanCollide = false
		Sound:Play()
		task.wait(4)
		Box.Transparency = 0
		Box.CanCollide = true
	end
end)

Thank you for your help and have a wonderful day! :slight_smile:

local Box = script.Parent
local Sound = Box:WaitForChild("Sound")

local Debounce
Box.Touched:Connect(function(Hit)
	if Hit:IsA("BasePart") and Hit.Name == "Handle" then
		if Debounce then return end
		Debounce = true
		
		local PrevTransparency = Box.Transparency
		local PrevCanCollide = Box.CanCollide
		Box.Transparency = 1
		Box.CanCollide = false
		Sound:Play()
		task.wait(4)
		Box.Transparency = PrevTransparency
		Box.CanCollide = PrevCanCollide
		
		Debounce = nil
	end
end)
1 Like
local Box = script.Parent
local Sound = Box:WaitForChild("Sound")
local db = true

Box.Touched:Connect(function(hit)
	if hit.ClassName == "Part" and hit.Name == "Handle" then
		if db then db = false
			Box.Transparency = 1
			Box.CanCollide = false
			Sound:Play()
			task.wait(4)
			Box.Transparency = 0
			Box.CanCollide = true
            task.wait(1) -- maybe
			db = true
		end
	end
end)
2 Likes

Now for some reason, whenever the box disappears, it deletes itself from the game.

Make sure its anchored. If can collide is set to false and it isnt anchored, it’ll just clip through the ground.

2 Likes

That fixed it! Thank you so much!

3 Likes

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