How do I detect if a user is on mobile?

Gonna give back true if a user is on a touch-screen device, so not reliable.

Ideally you should not be coding for a specific platform but instead make your code adapt to different inputs. For instance if I wanted to play a game on Roblox with controller and then later on I decide I don’t want to anymore, I don’t want to be treated as an Xbox player anymore. I want to be treated as a computer user.

3 Likes

Just what I was about to say. Lol.

2 Likes

This is the only thing available, see if tap, or keyboard is being used. You can use this to see last type of input.

UserInputService | Documentation - Roblox Creator Hub No API to get the exact device type, this is protected to CoreScripts.
See: UserInputService | Documentation - Roblox Creator Hub

UserInputService will tell you alot, I recommend using it…

2 Likes

Pretty sure that’s not the only way or the most efficient. Thanks for the contribution, though.

1 Like

You don’t detect if a device is mobile or not, there’s no accurate way to do it. You have to actively set the text depending on the player’s active input source. If the player used touch, change the text to “Tap”, otherwise “Click”.

1 Like

Keyboard enabled tells us that the user is not on mobile because they have a keyboard. Mobile users do not have a keyboard.

^ Is that correct?

11 Likes

Can you elaborate and not just drop a wiki link?

2 Likes

This is not accurate. There are windows devices that are purely touch screen.

1 Like

Correct… yes…
over30chars…

Then they would need the tap text instead of the click one, right?

Yes. A windows tablet will have the mobile controls and everything else mobile related.

Edit:
I think there’s some confusion here. Just to clarify,
Mobile = Phones and iPads/Tablets ?

Yea. Then why was I incorrect?

Don’t they have a keyboard too?
Edit: nope.

If your goal is to detect a mobile device, checking if they have a keyboard is not accurate because some windows tablets can come with keyboards. If the player had both touchscreen and keyboard, how would you determine if it’s mobile now? It leads to false positives.

1 Like

I recommend using UserInputService | Documentation - Roblox Creator Hub, as it may change depending on if someone, for instance, plugs in a controller.etc. I think this is what Whatever Floats Your Boat uses, pardon me if I’m wrong.

1 Like

You could use UserInputService.TouchEnabled
Hoever, ROBLOX have stated multiple times that your systems should work cross-platform.

1 Like

I guess so.

If I am trying to detect whether a player should be using a mouse and keyboard or a tapping, wouldn’t KeyboardEnabled be correct?

Is there any difference between a mobile device and a microsoft laptop with no keyboard or mouse?

You can solve this problem using the previously mentioned API to check changes throughout play. LastInputType. You can use it one time, or listen for changes on this, and adjust your UI accordingly.

This will solve this problem. It has an event for if it changes here

:eyes:

If you’re checking mouse and keyboard, use KeyboardEnabled and MouseEnabled

Yes, most mobile devices do not support usb accessories. All windows tablets with no keyboard and mouse can plug in external hardware anytime.

Oh… ok. That makes sense. :slight_smile: