PlatformProvider -- A means of (mostly) reliably acquiring the current type of platform the user is on

An Important Disclaimer

Normally, I wouldn’t put something like this at the top of a post - let alone before the actual focus of the post itself - but in this case, I have decided to make an exception due to the importance of this information.

PlatformProvider should [color=#ff5f5f]NEVER[/color] be used to perform platform-conditional actions that can alter how a user interacts with the game. Actions performed in the game should always match the context of whatever it is they’re doing and how they are doing it (e.g. what input device they are using should define how the game is controlled, not this module).

Misusing this module may severely damage your game’s reception and cause a whole slew of problems that could have been trivially avoided simply by doing things the right way. You have been warned.

PlatformProvider - What is it?

PlatformProvider is a simple module that will use various context clues to attempt to discern the overall platform type a user might be running. It was designed with edge cases in mind (e.g. if someone plugs in a keyboard and mouse into their Android device) and should ideally provide reasonable output reflective of the user’s current platform. Right now, it can return three values representing computers, xbox, and all mobile devices.

This module was made in light of the great number of requests for a method of acquiring the user’s platform. The use cases are vast, and so long as the precautions mentioned in the start of this thread are taken, this module should remedy most of the desires you all might have.

You can get the module here.

How do I use it?

The API is incredibly simple. Here’s an example script:

local PlatformProvider = require(path.to.PlatformProvider)
local CurrentPlatform = PlatformProvider.GetPlatform()

function DoSomethingWithThisInformation()
    if CurrentPlatform == PlatformProvider.Platforms.Mobile then
        -- Whatever it may be.
    end
end

The method this module uses to detect looks at the available hardware and uses a points system to gauge the best fit. Gyroscope and accelerometer both add 2 points to the mobile result, and keyboard, mouse, and gamepad add 1 point to the computer result.

If both end up being equal, there is a boolean (true/false) property of the module PlatformProvider.GuessFromTouch which will use the presence of a touchscreen as a last-resort means of discerning between mobile and computer. Its default value is true. Of course, a computer can be touch screen, so that is why this method is used as a last resort.

Of course, if you figure out anything that should be tweaked or run into bugs, just let me know and I’ll do what I can to fix it. Enjoy!

19 Likes

If we cannot rely on such a service to consistently and accurately determine the platform of the user, then it really is not helpful at all. This is not to discredit your work, I have personally investigated this very issue (like how to tell if someone is on mobile but has a keyboard connected, as you mentioned) and I know that there is no definitive way to determine this kind of thing.

The question to Roblox is, why are we still not allowed to see the platform of a user? Looking at the API, we know Roblox has this exact information, but they prevent us developers from reading it, which is incredibly stupid. Is it to encourage developers to design around control schemes and not platforms? Perhaps, and it would make sense for promoting generally good practice, but why does Roblox need to step in and make these decisions for us? There are without a doubt multiple use cases for knowing the platform, and not the control scheme.

1 Like

In a way, he has a point y’know. You really shouldn’t use platform-conditional actions just because their platform is that.

I’ve seen many players use mouse and keyboards with their phone / xbox, and there are people who have xbox controllers and touchscreens installed on their pcs. You can never know. :man_shrugging:

1 Like

The only real use-case for knowing a player’s platform is analytics, which roblox already satiates in their own analytics. If you rely on platform to determine how to handle UI, controls, etc then you are doing something wrong, The reason Roblox doesn’t want to provide developers with a way to know platform is because there’s not a big enough use-case to justify encouraging developers to hardcode controls based on platform.

If you need to adjust your UI’s layout, use the resolution.
If you need to setup the controls, use the active input devices (keyboard, mouse, touch-screen, etc.)

If you ignore platform entirely and only focus on the characteristics of the platform then your game will be platform-agnostic and also work with mixed controls (keyboard on phone, controller on pc, etc).

I believe this module is designed to target the few (if any) use cases where you might need the platform (e.g custom analytics), not to change controls, UI layout, etc based off platform (which is a very bad idea.)

4 Likes

Precisely this. Only passive things are really acceptable, things that are non-deterministic for the interaction of the game.

Some examples:
OK:

  • Using this module to display a user’s current platform in some sort of HUD or playerlist.
  • Using this module to grant certain content relevant to the platform the user is on that is cosmetic e.g. an Xbox sign-in item.
  • Using this module for analytics, for instance, reporting which bugs are more or less common on certain platforms. Anything that Roblox doesn’t provide already.

Not OK:

  • Using this module to alter the game’s mechanics to suit the platform (e.g. aim assist on mobile or xbox – this should focus on the controller being present, not the platform)
  • Using this module to edit player interaction or controls.
3 Likes