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

Why won't the debounce in the script work?

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

✓ Accepted answer

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)

1 comment

0

this is better, it actually explains it!

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)

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