How do I detect if a user is on mobile?

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:

I have a bluetooth keyboard for my phone

Wow… eleven month revival.

If the script is deciding whether or not to give you a mobile “tap” button or a desktop “e” button, the only deciding factor is whether or not you have a keyboard because even if you are on a phone and you have a keyboard, you can press e.

1 Like

Hi! Sorry for reviving this topic, but there’s actually several different methods. Here are some that you can try out.

  • Checking for keyboards (But there are keyboards for mobile devices too) KeyboardEnabled
  • Checking screen size ViewportSize
  • Checking the mouse size, the roblox chat uses this (Yes, mobile players have mouse objects) Mouse
  • Looking for touch Input TouchEnabled

Those are all the methods that I know and use, it really depends on what you’re doing. If I were trying to make UI more responsive then I’d use the camera’s viewport size, if I was looking for a specific input (for instance a gun system that needs to handle mobile input differently) then I’d use keyboard checks.

While these do work in some areas, it’s impossible to get a perfect prediction. Your best bet is to try to make your code as adaptable as possible.

3 Likes

Hey! Thanks for your response, this is a great point of discussion.

The main intent of this post was to figure out a way to determine wether or not a user needs to be presented with mobile friendly UI or PC friendly UI (as an example).

What I found was the easiest and safest method was using KeyboardEnabled and TouchEnabled, like you said in your post. Someone earlier pointed out that this could trigger a mobile output for a device such as a windows touch screen device but this didn’t really matter as they would need the touch friendly UI anyway.

I just wanted to quickly advise any readers to avoid the following method:

Camera.ViewportSize returns a Vector2 of the x and y of the screen size in offset. This is insecure if you’re checking what type of UI to give a player! (e.g “tap” or "click). If it hasn’t cliked for you, checking the screen size can cause big tablets to have a “click” and small laptops to have a “tap”, even though that’s the opposite to what we want them to have. Make sure that you’re using KeyboardEnabled and TouchEnabled, and maybe check it every couple of seconds to see if the user plugged in a mouse/keyboard or switched between tablet and pc modes on a touchscreen laptop.

1 Like

What I do is I use KeyboardEnabled, MouseEnabled, and TouchEnabled all at the same time to detect if the player is on mobile.

I understand, if you read the bottom of the post I described in more detail what I’d use each for. I agree, this shouldn’t be used to change what you’re describing. I use this method to help make UI more responsive, not to change the UI text.

Additionally, getting the mouse size and screen size are actually returning the same thing just in different value types.

Here’s an example of using Camera.ViewportSize.

2 Likes

If the game allows it, wouldn’t this interfere with Xbox?
Xbox players have a controller, not always a keyboard

This is not necessarily correct.

There’s many “rare” scenarios like:

  • The user is on mobile but has a Keyboard connected.
  • The user is on pc but doesn’t have a keyboard,

The documentation you linked also hints towards what I just said.

Check for more than just the keyboard, like if the player doesn’t have touch enabled and the player has a keyboard. then you can safely say their on pc.