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

01local enabled = false
02local debounce = false
03local Model = script.Parent.Parent.Parent.gas
04script.Parent.ClickDetector.MouseClick:Connect(function()
05    for i,v in pairs (Model:GetChildren()) do
06        if not debounce then
07            if enabled == false then
08                v.Smoke.Enabled = true
09                wait(3)
10                debounce = false
11                if v.Smoke.Enabled == true then
12                    v.Smoke.Enabled = false
13                end
14            end
15        end
16    end
17end)

2 answers

Log in to vote
1
Answered by
enes223 327 Moderation Voter
3 years ago
Edited 3 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.

01local enabled = false
02local debounce = false
03local Model = script.Parent.Parent.Parent.gas
04script.Parent.ClickDetector.MouseClick:Connect(function()
05    for i,v in pairs (Model:GetChildren()) do
06        if not debounce then
07            debounce = true
08            if enabled == false then
09                v.Smoke.Enabled = true
10                wait(3)
11                debounce = false
12                if v.Smoke.Enabled == true then
13                    v.Smoke.Enabled = false
14                end
15            end
16        end
17    end
18end)
0
this is better, it actually explains it! NarwhalAndMe 141 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
01local enabled = false
02local debounce = false
03local Model = script.Parent.Parent.Parent.gas
04script.Parent.ClickDetector.MouseClick:Connect(function()
05    for i,v in pairs (Model:GetChildren()) do
06        if debounce == false then
07            if enabled == false then
08        debounce = true
09                v.Smoke.Enabled = true
10                wait(3)
11                debounce = false
12                if v.Smoke.Enabled == true then
13                    v.Smoke.Enabled = false
14                end
15            end
16        end
17    end
18end)

Answer this question