Hey all, I’m sure this question has been answered before but I can’t seem to find it even through the forum search or Google.
I have a tool that I want to behave slightly different if a player is playing using the touch GUI that is active when playing on a mobile device. However, I can’t seem to find a recommended way to do this. I imagine just checking for the GUI in PlayerGui itself is kind of hacky and subject to break if Roblox renames it. Is there a better way to go about doing it?
Checking if a player is using the digital thumbstick IS checking based on their input methods.
I don’t know what exactly prompts Roblox to make it appear, but it doesn’t show up when I play on my touch screen laptop, and it does on my mobile device.
I have a tool that when holding down the mouse, makes your character rise into the air. This system is fine when using a KB+M or gamepad, but since moving around on a mobile device requires holding down on the screen, it also registers as holding down the mouse, which I do not want. Therefore, I want to do a check to see if this overlay is present and make the tool function differently if it is.
I also ran into this problem when coding custom mobile controls for weapons.
I found using LastInputTypeChanged worked best to avoid checking for the TouchGui.
If the player uses their mouse or touches their screen it’ll fire handleDeviceChange.
You can also modify the getDeviceType function to return “console” if it matches a gamepad InputType if needed.
local UserInput = game:GetService('UserInputService')
local function getDeviceType(inputType)
return (
inputType == Enum.UserInputType.Touch and 'mobile'
) or 'desktop' -- Assume device is a desktop
end
local function handleDeviceChange(newDeviceType)
if newDeviceType == 'mobile' then
-- Show mobile ui
elseif newDeviceType == 'desktop' then
-- Show desktop ui
end
end
local lastDeviceType = getDeviceType(UserInput:GetLastInputType())
handleDeviceChange(lastDeviceType)
UserInput.LastInputTypeChanged:Connect(function(newInputType)
local deviceType = getDeviceType(newInputType) -- Figure out what device the player is using
if deviceType == lastDeviceType then
return -- Avoid calling handleDeviceChange for the same device
end
lastDeviceType = deviceType
handleDeviceChange(deviceType)
end)