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

Why won't the debounce in the script work?

Asked by
Comqts 6
2 years ago

So basically, I'm trying to make it so when a button is pressed, the smoke turns on, That part works, however, I'm trying to make it so you can disable it with a debounce. If you can, please help.

local enabled = false
local debounce = false
local Model = script.Parent.Parent.Parent.gas
script.Parent.ClickDetector.MouseClick:Connect(function()
    for i,v in pairs (Model:GetChildren()) do
        if not debounce then
            if enabled == false then
                v.Smoke.Enabled = true
                wait(3)
                debounce = false
                if v.Smoke.Enabled == true then
                    v.Smoke.Enabled = false
                end
            end
        end
    end
end)

2 answers

Log in to vote
1
Answered by
enes223 327 Moderation Voter
2 years ago
Edited 2 years ago

You need to set the debounce to true because after the check is completed you only want it to work only once, right? If you want it to only work once then set the debounce to true after the debounce check because other iterations will check the if statement again and the debounce will be true, making it not pass the debounce check.

local enabled = false
local debounce = false
local Model = script.Parent.Parent.Parent.gas
script.Parent.ClickDetector.MouseClick:Connect(function()
    for i,v in pairs (Model:GetChildren()) do
        if not debounce then
            debounce = true
            if enabled == false then
                v.Smoke.Enabled = true
                wait(3)
                debounce = false
                if v.Smoke.Enabled == true then
                    v.Smoke.Enabled = false
                end
            end
        end
    end
end)
0
this is better, it actually explains it! NarwhalAndMe 141 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
local enabled = false
local debounce = false
local Model = script.Parent.Parent.Parent.gas
script.Parent.ClickDetector.MouseClick:Connect(function()
    for i,v in pairs (Model:GetChildren()) do
        if debounce == false then
            if enabled == false then
        debounce = true
                v.Smoke.Enabled = true
                wait(3)
                debounce = false
                if v.Smoke.Enabled == true then
                    v.Smoke.Enabled = false
                end
            end
        end
    end
end)

Answer this question