How to detect if Player is using a Phone?

How to detect if Player is using a Phone? :thinking:

32 Likes

Usually how they walk, since it is completely different from PC.

7 Likes

for mobile devices you can use this, https://www.robloxdev.com/api-reference/property/UserInputService/TouchEnabled. If you want the specifics of whether or not a user is on a phone you would have to compare screen resolutions paired with this.

12 Likes

Bad idea, this can also return true for laptops/PCs with touch-enabled screens.

In general, you shouldn’t write code for specific platforms. You should write code that is able to adapt to varying screen sizes and varying input modes.

Resources:


EDIT: removed incorrect code listing.

166 Likes

Be aware that Android devices do support controllers, so I wouldn’t advise checking for gamepads. Android also supports keyboards (like the BlackBerry KEY2), so it may also return true for that.

8 Likes

Again I don’t advise writing code for a particular device, it should be based on last input type and on screen size. The only reason why I personally would want to check for a particular kind of device (type) is for analytics purposes.

15 Likes

Or perhaps writing code specifically for the Xbox One, which has no mouse / keyboard support.

4 Likes

It also pretty much depends on usage/purpose of checking for a phone. What are the criteria? Maybe you want to setup touch controls, therefore you would set up touch controls given TouchEnabled. On the other hand, you might want to setup devices with accelerometers, so you’d set it up when one is sensed, etc.

2 Likes

No… that’s the exact opposite of what you should be doing. Let’s try this explanation again more thoroughly.

You really do not write control/UI code for a particular device. The Xbox One has controller as its main input type, so what you’re really wanting to do is implement your game to work with the controller input modes, and that your game will adjust the UI labels and size of the UI appropriately when it detects the last input type is controller / the player is playing on a 10-ft screen (there is API for this).

If your game works on controller, it works for Xbox One, for PC + controller, and for any future supported device with a similar controller form factor.

The end goal, if you want the biggest reach and the maximum user experience, is to have this working in your game:

  • Touch support + appropriate touch UI (make sure no keybind labels are shown, make sure buttons are big enough for the lowest screen sizes on mobile, and think about portrait vs landscape modes)
  • Controller support + appropriate controller UI / labels (controllers can’t click on things as easily, use shortcuts and easy to navigate grids/lists)
  • Keyboard + mouse + appropriate keyboard/mouse UI (this is probably the easiest to implement, and you probably already have a lot of intuition about this)
  • Your UI/controls should seamlessly, at any moment in time be able to switch between these modes when the input mode changes / when the screen size changes. Yes, even from touch to controller, and especially from controller to keyboard/mouse and vice versa.

For a good example of all of this, see Egg Hunt 2018. Everything switches based on input mode (i.e. try playing with keyboard with a controller plugged in, and then pick up your controller and play with that, and back again) and some things also change around when you change your window size / when you flip your phone/tablet to portrait mode, etc.

18 Likes

My personal experience is this:

while true do
		wait()
		if game:GetService("UserInputService").TouchEnabled and game:GetService("UserInputService").KeyboardEnabled == false then
			print("Mobile User")
		else
			print("Probably not a Mobile User")
	end
end

It’s probably not the best, but it’ll do. Like others said, there is some false positives, but for the most part, someone who has touch enabled without a keyboard will be a Mobile User. And for what I’m doing, is just giving the Mobile Users a special car, and disallowing them from using a GUI that doesn’t properly work for them, while I work on a GUI that works on Mobile / PC / Xbox.

17 Likes

It’s annoying that it doesn’t always work, I tried if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled then, but i got players complaining that they are on mobile and don’t have mobile controls, and now i tried if UserInputService.TouchEnabled and not UserInputService.MouseEnabled then and people say they have mobile controls on laptops

8 Likes

Sorry to continue this conversation so long after the last reply but you could also use a ScreenGUI. You can put a ScreenGUI into StarterGUI and get the absolute size of it. You can then compare the GUI’s absolute size to a device size and see which device it is.

EDIT: only works if the player’s game is fullscreen

1 Like

This is a very bad solution, the resolution of a device is in no way an indicator of what type of device it is

2 Likes

as I said, it only works while full-screen. it would not be a permanent solution.

also, touch-tap and touch-enabled are still working solutions. true, it may get touch-enabled on touchscreen computers but in some cases, such as a game I am making, that is fine. For me, I needed to detect if a player was on mobile in order to switch to a different placement script in my sandbox game. Since my mobile script also works with touch-enabled computers, it is okay if it gets activated for people using tablet mode on a computer.

1 Like

Hey, I read the documents, how would I use it? I tried to use it and its not workin

1 Like

The documentation has a very generous set of code samples. Recommend reading through the documentation.

1 Like

You could just make a gui asking if their on a mobile or not? Normally this works, but you should just use
game:GetService(“UserInputService”).KeyboardEnabled or MouseEnabled

1 Like

easy way is to use local Mobile = plr.PlayerGui:FindFirstChild(“TouchGui”)
That basically search for the joystick and it is easy to use

9 Likes

fr fr, let roblox do the detection job for you lool, other than all of these nerds trying to create complex ways to detect that

5 Likes

Minimalistic programming lol.

Anyways just do an automated detection and, in case that’s wrong, just add a setting so they can toggle PC, mobile or other displays.

2 Likes