How do I detect if a user is on mobile?

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:

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.