How to detect if Player is using a Phone?
Usually how they walk, since it is completely different from PC.
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.
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.
Edited out old content
EDIT: I no longer recommend the code below for anything other than analytics, it is very easy to misclassify devices. Your code should always react on-the-fly to different screen sizes and input modes, as per the resources above.
If you have to know if something is a mobile device (i.e. primary Touch as input mode), use this check instead, it is more robust:
local UIS = game:GetService("UserInputService") local GuiService = game:GetService("GuiService") if UIS.TouchEnabled and not UIS.KeyboardEnabled and not UIS.MouseEnabled and not UIS.GamepadEnabled and not GuiService:IsTenFootInterface() then -- mobile device end
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.
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.
Or perhaps writing code specifically for the Xbox One, which has no mouse / keyboard support.
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.
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.
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.
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
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
This is a very bad solution, the resolution of a device is in no way an indicator of what type of device it is
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.
Hey, I read the documents, how would I use it? I tried to use it and its not workin
The documentation has a very generous set of code samples. Recommend reading through the documentation.