Click & Hold to Confirm UI

I’m trying to make a UI button that you need to click and hold the button for x amount of seconds for the function to fire. Would it be a better idea to use a key-bind or is this a good idea too? If it is a good idea, how would I go about scripting it?

You’d use inputBegan and inputEnded

Once it’s began, set a variable called ‘holdingMouse’ to true (make sure this variable is defined at the top of your code)

Once it’s ended, set the same variable ‘holdingMouse’ to false

Then you can use RenderStepped to do whatever you need to do so long as ‘holdingMouse’ is true.

3 Likes

I you wanted to do this, you could make a variable outside of a mousebutton1down and mousebutton1up event, with the down checking that the variable is true for x amount of time, and if the up event fires and changes the variable to false, then the down function would stop running.

Edit: Included an example

local button = gui.Button -- GUI Button

local timeLimit = 3 -- Time Limit in seconds

local mouseDown = false

button.MouseButton1Down:Connect(function()
  if mouseDown then return end

  mouseDown = true

  local i = 0

  while i < timeLimit do
    if not mouseDown then return end

    i = i + 0.25
    wait(0.25)
  end

  -- Code here
end)

button.MouseButton1Up:Connect(function()
  mouseDown = false
end)
1 Like