Detecting if a GUI Object has been touched by a mobile user with UserInputService?

I’m trying to make a mobile button that fires a function if you hold the GUI button, however I’ve stumbled upon this problem. As far as I’m aware there’s no such Event that detects if a GUI object has been touched rather if you tap it or hold it for a long time (Examples below). Are there any ways to detect if a GUI Object has been touched by a Mobile user with UIS specifically?

MobileButton:TouchLongPress:Connect(function (TouchPositions, GameHandledEvent)
MobileButton:TouchTap:Connect(function (TouchPositions, GameHandledEvent)

--// What I'm looking for is something like this
MobileButton:TouchStarted:Connect(function (InputObject, GameHandledEvent)

I think you could get some use out of ContextActionService.

I believe .MouseButton1Down or .MouseButton1Click works fine with mobile, but I may be wrong. What exactly are you trying to achieve? Just when the touch begins? A little confused :joy:

If you specifically want UIS, you can also detect using UIS.InputBegan if the InputType is on mobile and whether it is a touch, along with it’s position. Pairing this with .InputEnded can probably achieve whatever you need :slight_smile: ?

That’s not how ContextActionService works. It uses the exact same input types as UserInputService.

1 Like

I’m basically looking into creating a Sprint button. So it fires a function whenever the player has touched the GUI Object and disconnects the function if the players releases the button.

I would probably use UIS.InputBegan and UIS.InputEnded to achieve this. You can get the position of the tap, and check this against the absolute positions of the button to determine whether the user pressed in the right place

1 Like

Is there a cleaner way of doing this?

Oh, nevermind. Heh, found a solution. Here’s a code for those who’ll need this in the future.

YourButton.TouchLongPress:Connect(function (TouchPositions, TouchState) 
 if TouchState == Enum.UserInputState.Begin  then
 --Connect function
 elseif TouchState == Enum.UserInputState.End then
--Disconnect function

The touch functions part of GuiButtons are essentially forks of the ones from UserInputService, just tailored to work with the button only. About the same functions exist in UserInputService. There’s also GetObjectsAtPosition which allows you to determine what objects are at a certain point. There’s even some code samples for Mouse and Touch input.


It doesn’t. If it does, it shouldn’t or rather you shouldn’t use it. That’s almost abusing the API and it’s not proper code. Why would you use mouse events for devices that don’t have one? The proper way to handle mobile input is via touch events.


I was just suggesting that if a button is already coded to work with those events, it doesn’t need anything else as they should work already on mobile.

It seems a bit backwards to write lots of UIS connections for something that was already handled by the Mouse connections already written; but I didn’t realise that this was an unintended feature and abuse of the API. I will change the way I work with mobile in the future! Thanks for clarification :slight_smile:

1 Like

Might want to mark that post as the solution (click the check box), so that others know which post they need to look for if they experience similar problems, as well as others who wish to attempt to help you know that you’ve fixed your problem.