Is there a way to detect if a gui button is being held down (Please read the rest of the thread)

Lemme specify again, i want to detect if a button is being held down, not to find out which function makes you hold down a gui button, i already know MouseButton1Down1 What i want to do is check if a button is being held like Button.MouseButton1Down == true (thats not an actual if statement but you get my idea). Theres a loop that runs every 5 seconds to check if a player is holding down the button if that answered any question you had.

1 Like

“Gui Button” i said it multiple times.

Yeah, you can use the .MouseButton1Down and .MouseButton1Up events to find out if they are held down or not. Here’s an example:

local button = script.Parent
local timeDown, isDown, connection
	if connection then
		return -- if for whatever reason the .MouseButton1Down event fires multiple times then return
	isDown = true
	timeDown = tick()
	connection = button.MouseButton1Up:Connect(function()
		if connection then -- you shouldn't need this but just to be safe
			connection:Disconnect() -- avoid memory leak and avoid it being fired multiple times
			connection = nil -- set the variable to nil because disconnecting it doesn't actually set it to nil
			if isDown then -- if the button is down
				isDown = false
				print('Button was held for '..(tick() - timeDown)..' seconds.')

Edit: to cater to your loop part, since we’re setting a variable in the scope of the loop and the connection, we can use that variable in the loop.

while true do
    print('Button down: '..tostring(isDown)) -- if down then would print true, else prints false

doable but alot of work when considering that i have to change many scripts with it. I’ll use it as a last resort if there arent anymore solutions

You could use UserInputService:IsMouseButtonPressed and then use the :GetGuiObjectsAtPosition function instead? Since :GetGuiObjectsAtPosition returns a table, you can search through the table for the specified UI element.

local mouse = localPlayer:GetMouse()
local uiElement = script.Parent

while true do
    if userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then -- if it's down
        local x,y = mouse.X, mouse.Y
        local objects = playerGui:GetGuiObjectsAtPosition(x,y)
        if table.find(objects, uiElement) then
            print('User\'s mouse is down on',uiElement)

Only downside to this method is that if their mouse button is down and they drag their mouse into the element, it will still fire.


does it work with .Touched too? because that would be perfect. Also cant you just specify the button and use absolute position to check if its on it?

Do you mean with parts? If so then no, but you could probably use :GetTouchingParts for baseparts. Unless you mean if their mouse is on a part, then you could use mouse.Target.

Yes but you’d have to factor in the absolute size as well, and then you have to factor in rotated elements which :GetGuiObjectsAtPosition does AFAIK.

im talking about UserInputType.Touch

Ah, my bad. Yes, you should be able to.

1 Like

Thats perfect and thanks alot! wait havent you helped me before on something?

1 Like

No worries.

I might have, I try to offer solutions on here periodically.

ah doesnt seem to work, ill try to find a way around this