Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my Debounce not working correctly?

I usually make my scripts with how I'm setting up the debounce now, however, it's not working.

local p = script.Parent

function t(h)
	local hu = h.Parent:FindFirstChild("Humanoid")
	local db = true
	
	if hu then
		
			
			db = false
			
			p.Color = Color3.new(255,0,0)
			p.Sound:Play()
			wait(3)
			local b = Instance.new("Explosion")
			b.Parent = game.Workspace
			b.Position = p.Position
			p:Destroy()
			
			db = true
		end
	end


p.Touched:connect(t)

The sound keep replaying, and I'm not sure why. Any ideas as to why this is happening?

1 answer

It's because your debounce isn't doing anything at all.

local p = script.Parent
local db = true

function t(h)
    local hu = h.Parent:FindFirstChild("Humanoid")

    if hu and db then

            db = false

            p.Color = Color3.new(255,0,0)
            p.Sound:Play()
            wait(3)
            local b = Instance.new("Explosion")
            b.Parent = game.Workspace
            b.Position = p.Position
            p:Destroy()

            db = true
        end
    end


p.Touched:connect(t)

Now, the code block with your sound won't be able to run until debounce is set back to true. (I put debounce outside of the function because it's normal convention so it's not ONLY set locally for the function and can be detected by the entire script).

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