roblox debounce

local debounce = false

local function onButtonClicked()
    if not debounce then
        debounce = true
        -- Perform action here
        print("Button clicked!")
        wait(2) -- Wait for 2 seconds
        debounce = false
    end
end

-- Assuming the button click event
button.MouseButton1Click:Connect(onButtonClicked)
  1. local debounce = false: Initializes a variable debounce to false. This variable will be used to prevent rapid consecutive button clicks.

  2. local function onButtonClicked(): Defines a function named onButtonClicked which will be executed when the button is clicked.

  3. if not debounce then: Checks if debounce is false. This conditional statement ensures that the code inside the if block is only executed if debounce is false, preventing multiple rapid clicks.

  4. debounce = true: Sets debounce to true to indicate that a click has been registered, preventing further rapid clicks until it's reset.

  5. print("Button clicked!"): Placeholder for the action to be performed when the button is clicked.

  6. wait(2): Pauses the execution of the code for 2 seconds. This simulates some action being performed after the button click.

  7. debounce = false: Resets debounce to false after the action and the wait period are completed. This allows the button to be clicked again.

  8. button.MouseButton1Click:Connect(onButtonClicked): Connects the onButtonClicked function to the button's MouseButton1Click event. When the button is clicked (MouseButton1Click), the onButtonClicked function will be called. ```