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

How do I debounce a module script with a while loop in it?

I have the following script inside a button:

local click = script.Parent.ClickDetector
module = require(script.Parent.Parent.functions)
local debounce = false
local blox = workspace.iBlox

click.MouseClick:connect(function()
	if not debounce then
	debounce = true
	module.StartUp(blox)
	wait(5)
	debounce = false
	end
end)

The StartUp() function is in a module:

function module.StartUp(blox)
	local on = blox.TurnedOn
	local gui = blox.Screen.StartUp
	local rotate = gui.Frame.ImageLabel
	local y = 1 
	local button = blox.onoff
	rotate.Rotation = 0
	
if on.Value == false then
		on.Value = true
		button.Material = Enum.Material.Neon
		gui.Enabled = true
		wait(2)
		rotate.Visible = true
		y = 1
			while y < 200 do
				y = y + 1
				rotate.Rotation = y
				if on.Value == false then
					break
				end
				wait()
			end
else
		button.Material = Enum.Material.SmoothPlastic
		on.Value = false
		gui.Enabled = false
		rotate.Visible = false
	end
end

I want it to debounce so the button wont do anything until 5 seconds pass after it's been pressed but it will go over the whole function and more seconds pass, is there a way I could work around this?

1 answer

local debounce = true

--In your function:

if not debounce then
        return
    end
debounce = false
wait(5)
debounce = true

Not 100% sure if I answered your question correctly. Please tell me if there is any way I can improve it!

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