Mobile detector

I want a code which runs only if the player is using a mobile device

local OnMobile = -- I need help what should I put here to make it work?

if OnMobile == true then
   -- some random stuff I need for my project
end

I think UserInputService is your answer.

local uis = game:GetService("UserInputService")

if uis.TouchEnabled then
	print("Mobile")
end

you can check touch enabled

1 Like

You can use UserInputService, but this can rarely give false positives.

local OnMobile = false
local UserInputService = game:GetService("UserInputService")

-- Something like this, but i may have spelt some things wrong.
-- We check for the keyboard not being enabled because touchscreen monitors and laptops exist.
if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled then
    OnMobile = true
end
1 Like

For touchscreen laptops, wouldn’t UserInputService.KeyboardEnabled return true since it is a laptop which has a keyboard?

Yes. That’s why it inverses it’s value.

1 Like

as long as they’re considered, KeyboardEnabled is for PC and other devices that supports the keyboard, it tries to detect every input devices

1 Like

You would need a few different checks to ensure that it is always correct. You can check TouchEnabled of UserInputService, but touchscreen computers will then be considered mobile devices. So you’d need a few checks like this:

local uis = game:GetService("UserInputService")
if not uis.MouseIconEnabled and uis:GetLastInputType().Name == "Touch" then
    -- user is on mobile
end

umm I think there would be like a chance for computer to also be true

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.