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

How do you detect if a mobile player has tapped the screen?

Asked by 3 years ago

I really have given up all hope to add mobile support to my game since my small brain doesn't know how to detect mobile input, please tell me how, i dont understand the dev forums, please explain to me how to do it like im 2 or something

0
What are you trying to do when the mobile player touches the screen> bulder251 26 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

UserInputService is designed to handle this type of interaction, and it includes gamepad controls as well as mobile. It's only available in localscripts. You can access it by calling

local UserInputService = game:GetService("UserInputService")

First, you want to start with an input began function, so you can sense when the user starts typing/clicking.

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    --code in here
end)

Here, the input is what the player is entering, and the game processed event senses if the player accessed a game element with the input, such as clicking a button or opening the chatbox. The game processed event is basically only used if you want the player to not be able to perform the action if they are doing something else.

Next, you want to check if the input type is the one you want to be using. This determines if the user is using a keyboard, mouse, gamepad, etc. In your case you need the touch input type, which senses if the player taps the screen on a mobile device. This can be accomplished with the following code:

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.Touch then
        --Tapped the screen
    end
end)

Now all you need to do is insert the code for whatever you want to do in your game inside the if statement.

Alternatively, if you want to sense different types of touches/swipes on mobile, you can use this dev forum page: https://developer.roblox.com/en-us/api-reference/event/UserInputService/TouchStarted and the see also section. It includes different mobile inputs such as swipes, rotations, and more.

Another thing that might be useful to you is sensing if a player is on a mobile device.

if UserInputService.TouchEnabled then
    --User is using a touchscreen
end

I'm not an expert on this stuff, so it's better for you to check the dev forum pages or watch userinputservice tutorials on youtube, but hopefully you found this helpful :)

0
thanks! Radiant_Sparkles 69 — 3y
Ad

Answer this question