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

Why is my debounce not working? [Solved]

Asked by 9 years ago
local debounce = true

script.Parent.Touched:connect(function(part)
    local h = part.Parent:FindFirstChild("Humanoid")
     if h ~= nil and debounce == true then
        wait()
        game.ServerStorage.t1:Clone().Parent = game.Workspace
        script.Parent.p:Play()
        local tp1 = game.Workspace.t1
        wait()
        script.Parent.BrickColor = BrickColor.new("Black")
        debounce = false
        --[[The following lines are optional in case you want a timer for a button to reactivate.

        wait(5) --Cooldown time. Can be changed to anything depending on what you want.
        script.Parent.BrickColor = BrickColor.new("Really blue") --Change the color to whatever you
                                                                 --want.

        debounce == true --This re-enables the script. Don't mess with this unless you want to break
                         --the script.

        --]]
    end
end)

Sorry for the slightly messy code. Basically, I want this button to disable itself after you touch it WITHOUT disabling the script, because I want a "cooldown" time for scenarios later in my game. I would very much appreciate assistance on this, as I am not too sure how debouncing works. Thank you!

1 answer

Log in to vote
1
Answered by 9 years ago

Here you go!

The explanation is in the script. :] Also, you should make the debounce a little longer because it's not really a debounce if you're only using it for not even a second.

local debounce = true 

script.Parent.Touched:connect(function(part)
    local h = part.Parent:FindFirstChild("Humanoid")
     if h ~= nil and debounce == true then
        wait()
    debounce = false --ADD THIS HERE BECAUSE THEN WHEN YOU TOUCH THE PART, YOU WON'T BE ABLE TO ACTIVATE IT UNTIL YOU CHANGE THE DEBOUNCE TO TRUE AGAIN.
        game.ServerStorage.t1:Clone().Parent = game.Workspace
        script.Parent.p:Play()
        local tp1 = game.Workspace.t1
        wait() 
        script.Parent.BrickColor = BrickColor.new("Black")
        debounce = true -- CHANGES TO TRUE SO YOU CAN REACTIVATE IT.

        --[[The following lines are optional in case you want a timer for a button to reactivate.

        wait(5) --Cooldown time. Can be changed to anything depending on what you want.
        script.Parent.BrickColor = BrickColor.new("Really blue") --Change the color to whatever you
                                                                 --want.

        debounce == true --This re-enables the script. Don't mess with this unless you want to break
                         --the script.

        --]]
    end
end)
0
A tip, for a short cut. Instead of debounce == true, just do debounce by itself since debounce means true so it will run. EzraNehemiah_TF2 3552 — 9y
Ad

Answer this question