How to detect clicking on a button with UserInputService

Hi! I am trying to detect when the player clicks a button. How would I do this using only UserInputService, not the button events? Thanks.

1 Like

Take this

local ui_s1 = game:GetService("UserInputService")
    ui_s1.InputBegan:Connect(function(tp_t1, tp_t2)
    	if tp_t2 == true then
    		return
    	end
    	if tp_t1.UserInputType == Enum.UserInputType.MouseButton1  then
    		print("A")
    	end
    end)
1 Like

This only checks if the player clicked. I need to detect when the player clicked on a GUI button. Sorry for the misunderstanding.

You can try checking if the player’s mouse position is within the perimeter of the button.

How would I do that? (the button is inside a frame and uses Scale, not Offset)

There’s this:
GuiObject.MouseEnter
However it might fall under a button event.

Yeah, it’s a button event. I want it to be compatible with Touch Events. Any way to do that?
I know there is StarterGui:GetGuiObjectsAtPosition() or something like that but it doesn’t seem to work.

If all you need is for a button to be compatible with touch devices, there’s GuiObject.TouchTap.

Otherwise, you can run a loop to constantly check the player’s mouse position.

1 Like

Okay. I’ll try that and let you know if it works.

Hi, can I ask why you would want to do this?

button.Activated should be compatible with touch devices.

In any case, it’s not a very efficient way, I don’t see why you would want to, but it’s possible:

I would highly recommend using the button events.

2 Likes