Is this a good way to know if a player is on mobile?

if player.PlayerGui:FindFirstChild("TouchGui") then
-- Is on mobile because it has the touch controls gui
end

Everyone on the contrary uses UserInputService to know if the player has a touch screen and does not have a keyboard, but can this be a more efficient way?

1 Like

The issue of detecting what hardware a user is playing with has been an ongoing issue for years. There’s people on mobile with keyboards and mice, people on consoles with keyboards and mice, and people on computers playing with touch screens. You get the idea, I digress.

I usually assume that everyone with a touch screen (game:GetService("UserInputService").TouchEnabled being true) is using a mobile device of some sort. Furthermore, I usually scale the entirety of my UI scheme based on if either the X or Y dimension is less than some threshold (usually X < 800 or Y < 600).

2 Likes

That’s a good way to adjust the UI, but I don’t know if that answers my question.

If you’re only adding the TouchGui object to the player’s UI if they’re on mobile devices (detected in whatever way you have already implemented) then I would say that your way is just fine. It’s hard to determine without seeing your platform detection code. Could you post it here?

Edit: Actually, is the TouchGui the UI Roblox adds by default with the jump button? If so you should be good with doing what you posted.

So, it’s as simple as checking if the player’s PlayerGui contains a child named TouchGui, which is automatically added if the player is on mobile devices as well as the ChatGui is added for example.

The problem is that I use it that way, but I don’t understand why others prefer to use UserInputService with other combinations although it may be less accurate.

What you’re doing is piggybacking off from the standard player scripts that normally determine whether to show touch controls, in an unsupported way.

I wouldn’t recommend doing this in your current way because Roblox may change how the touch controls are displayed in the future, such as by renaming the ScreenGui you’re looking for, which can lead to your code assuming everybody is on a computer or game console.

Instead you should dig through the player scripts and figure out how it determines whether to show touch controls, so you can do it yourself.

2 Likes

The inability for developers to check what device the user is using isn’t an issue, I’d say it’s almost intentional. You should never really have a need to check a player’s device. There may be cases where you do want device checks (analytics, minimalisation, etc) but at the same time it’s also a good way of generating bad habits (lack of systemic abstraction).

1 Like

I probably should’ve used the word “puzzle” rather than issue as I made it sound like it’s Roblox’s fault when it’s not.

1 Like