Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Help! I want to make sure a divice is mobile in my game! how do i do it?

Asked by
Tizzel40 243 Moderation Voter
6 years ago

so I want the game to check what device the player has. If its on a mobile device then the gui will pop up. If not then the gui will NOT pop up. Is there a way to do that?

0
Yes, there is a way. Good day. User#19524 175 — 6y
0
... Tizzel40 243 — 6y
0
What? Your question is a yes or no question, and the answer will be a yes or no answer. User#19524 175 — 6y
0
yea i guess...... Tizzel40 243 — 6y

1 answer

Log in to vote
0
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:

if game:GetService("UserInputService").TouchEnabled then
    script.Parent.Visible = true
end

Edit #1: If you don't want touch screen computers to use this, then also check if KeyboardEnabled is false, like in this example:

local UIS = game:GetService("UserInputService")
if UIS.TouchEnabled and not UIS.KeyboardEnabled then -- If there's a touch screen with no keyboard then....
    script.Parent.Visible = true
end

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:

-- Server (Non-local script)

local isOnMobile = {}
local event = Instance.new("RemoteFunction", game:GetService("ReplicatedStorage"))
event.Name = "MobileData" -- Change event name if you have other events.
game:GetService("Players").PlayerAdded:Connect(function(p)
    local isMobile = event:InvokeClient(p)
    if p and isMobile ~= nil then -- Alot of issues happen with RemoteEvents/RemoteFunctions. Good to check this stuff.
        isOnMobile[p.UserId] = isMobile
    end
end)

-- Client (Local script)
game:GetService("ReplicatedStorage"):WaitForChild("MobileData").OnClientInvoke:Connect(function()
    local isMobile = false
    local UIS = game:GetService("UserInputService")
    if not UIS.KeyboardEnabled and UIS.TouchEnabled then
        isMobile = true
    end
    return isMobile
end)
0
thank you. Tizzel40 243 — 6y
0
That won't do what OP wants. This is true if the user has a touch screen computer. User#19524 175 — 6y
0
? Tizzel40 243 — 6y
0
You can also check if the user doesn't have a keyboard if you want that too. I'll edit this to have KeyboardEnabled fighter169mobile 123 — 6y
View all comments (15 more)
0
wait really!? becase for my game i dont want my buttons to appear both on mobile and PC to prevent auto clicking... so this answer is...... wrong? Tizzel40 243 — 6y
0
I edited the post with it also checking KeyboardEnabled. If you don't want double clicking, you should be fine using `else` if there's any exception. fighter169mobile 123 — 6y
0
yea i eddited it to in my script.wait THIS is a script right? not a local script? Tizzel40 243 — 6y
0
Also `not UIS.KeyboardEnabled` is similar to `UIS.KeyboardEnabled == false`, it is just less likely to error this way. fighter169mobile 123 — 6y
0
Only works with local scripts. I'd use a RemoteFunction to get what property each player has if you're using a server script. fighter169mobile 123 — 6y
0
hmm ok Tizzel40 243 — 6y
0
I'll edit this to add another example with a server script. fighter169mobile 123 — 6y
0
Updated post fighter169mobile 123 — 6y
0
If anyone can't read line 14: game:GetService("ReplicatedStorage"):WaitForChild("MobileData").OnClientInvoke:Connect(function() fighter169mobile 123 — 6y
0
note:My game is not experimential mode and not filtering enabled Tizzel40 243 — 6y
0
all of all thoes edits which is totaly the best one.edit 2 or 1,3 Tizzel40 243 — 6y
0
Still should be okay fighter169mobile 123 — 6y
0
"not experimential mode and not filtering enabled" both of these are the opposite, what do you mean? fighter169mobile 123 — 6y
0
Line 14 will error. User#19524 175 — 6y
0
Also, the parent argument is deprecated, do not give OP deprecated code. User#19524 175 — 6y
Ad

Answer this question