Press and hold action

I want to figure out how to press and hold a button (On a SurfaceGUI).

If the button has been held down for at least 2 seconds I want it to bring a submenu (I have something for this already, and it’s submenu UI and animation)

This is an example of IOS’ menu.

I do not know how I can make the script see that I’m pressing and holding the textbutton for at least 2 seconds.

This is the button on the SurfaceGUI
image

If you require further explainations, I will try that. My explainations are not the best.

For this use MouseButton1Down And MouseButton1Up

Try this:

local timeBetween = 0
local down = false

script.Parent.MouseButton1Down:Connect(function()
    down = true
    task.spawn(function()
        repeat wait(0.1)
            timeBetween += 1
        until timeBetween == 2 or down == false
        if timeBetween ~= 2 then return end
        print("Mouse was held down on button for 2s")
    end)
end)

script.Parent.MouseButton1Up:Connect(function()
    timeBetween = 0
    down = false
end)

BTW, this was written on mobile so it is untested (but should check to see when the button was held for 2 seconds) and could contain errors

4 Likes

You can use the MouseButton1Down and MouseButton1Up events to check for how long the mose has been held down for.

https://developer.roblox.com/en-us/api-reference/class/TextButton

Thanks it worked for me perfectly as I needed it to be