Answered by
6 years ago Edited 6 years ago
The best way to check if a device is on mobile is to check if the screen can be touched using UserInputService
with the TouchEnabled
property.
Here's an example of making the Parent object visible when the device is on mobile:
1 | if game:GetService( "UserInputService" ).TouchEnabled then |
2 | script.Parent.Visible = true |
Edit #1: If you don't want touch screen computers to use this, then also check if KeyboardEnabled is false, like in this example:
1 | local UIS = game:GetService( "UserInputService" ) |
2 | if UIS.TouchEnabled and not UIS.KeyboardEnabled then |
3 | script.Parent.Visible = true |
Edit #2: If you have a server script (not a local script), then I'd use a RemoteFunction. The issues I've had with RemoteFunctions is that you need a certain type of function. This example records what device everyone is using to the server when the player joins:
04 | local event = Instance.new( "RemoteFunction" , game:GetService( "ReplicatedStorage" )) |
05 | event.Name = "MobileData" |
06 | game:GetService( "Players" ).PlayerAdded:Connect( function (p) |
07 | local isMobile = event:InvokeClient(p) |
08 | if p and isMobile ~ = nil then |
09 | isOnMobile [ p.UserId ] = isMobile |
14 | game:GetService( "ReplicatedStorage" ):WaitForChild( "MobileData" ).OnClientInvoke:Connect( function () |
15 | local isMobile = false |
16 | local UIS = game:GetService( "UserInputService" ) |
17 | if not UIS.KeyboardEnabled and UIS.TouchEnabled then |