How to detect if Player is using a Phone?

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.

18 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

9 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

10 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

Update: UserInputService has a cool feature UserInputService.TouchEnabled

local userInputService = game:GetService("UserInputService")

if userInputService.TouchEnabled then
	print("The user's device has a touchscreen!")
else
	print("The user's device does not have a touchscreen!")
end

which is good enough to detect touch enabled devices

1 Like

that is not good some display’s have touch screen use touch-enabled and keyboard enabled

1 Like

I feel like @skyblox7862’s solution is much more reliable.

2 Likes
local UserInputService = game:GetService("UserInputService")

local function checkDeviceType()
    local screenSize = workspace.CurrentCamera.ViewportSize
    if screenSize.X >= 1024 and screenSize.Y >= 768 then
        return "iPad or larger tablet"
    elseif screenSize.X >= 800 and screenSize.Y >= 480 then
        return "Phone with a larger screen"
    else
        return "Smaller phone"
    end
end

UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Touch then
        local deviceType = checkDeviceType()
        print("User is using a " .. deviceType)
    elseif input.UserInputType == Enum.UserInputType.Keyboard then
        print("User is using a keyboard")
    end
end)

Edit: Dang, that solved was quick. Didn’t have time to dig through my stuff. My bad.

Mikhali Yes, I understand Bluestacks exists. At one time knowing this was important, I even made an offset plugin so all my GUIs would work on any screen. However while I was creating that plugin I figured out how this was really done. This is still useful for other reasons and why I had problems finding it. It is buried in my not so useful pile…

For those looking to achieve what I stared doing here, creating generic GUIs … Here is a 1:11 video on how to make your GUIs fit any device. Yes, it is short and believe it or not it actually works.
How To Make GUI Fit All Devices In Roblox Studio (Easiest Method)

In this video you will see him setting a very small GUI … more of a popup. The trick to this is, is to set every single frame, button and picture (basically anything with a size and location) the way he will show you.

11 Likes