Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my Debounce not working correctly?

Asked by 7 years ago

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

Log in to vote
1
Answered by 7 years ago

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

Ad

Answer this question