How can I get the part a mobile player has touched

If the client is on PC, you can use Mouse.Target which returns the part the client is hovering over. Although how would I go about doing it for mobile players?

I’m making a system where PC players can interact with a button that carries out a function, on PC, you press E when you’re close to it, that’s done. Although mobile players can’t press E, so I want to make it so they can tap the button and it’ll send it through the system which does what it needs to. I’m currently picking up the clicks by using UserInputService.InputBegan and then using the InputObject to determine whether the UserInputType was Enum.UserInputType.Touch.

Do I just need to use client side clickdetectors which only exist for clients with TouchEnabled true? It isn’t really easy to link though, that’s the problem.

Please get back to me as soon as you can.

3 Likes

Mouse.Target does work for mobile devices, but it works a little differently, the mouse.Target would essentially be the last part the player clicked on in the workspace. So what your gonna have to do is use mouse.Target in combination with some functions from the UserInputService like this:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
UIS.TouchTapInWorld:Connect(function(vector2Pos,ProcessedByUI)
   if not ProcessedByUI then
      if Mouse.Target then
        print(Mouse.Target)--part that is being clicked on
      end
   end
end)

Another option would be to use billboard guis, with a textbutton parented to the billboardGui

9 Likes

Using mouse.Target is a bit of a hack, rather it’s canonically incorrect, and should not be used on devices without a mouse. You should instead use the Vector2 coordinates provided to you by the function TouchTapInWorld and construct a unit ray via ViewportPointToRay, then extend that forward.

2 Likes

Its not really a hack, all mobile devices on roblox have a simulated mouse,nonetheless raycasting is a viable option but its a bit tricky to grasp with mathematical knowledge on rays. I doubt roblox is gonna depreciate it though because on devhub they said its still around because alot of games use it to this date.

2 Likes

You can get the CurrentCamera.Ray to see what the player is looking at, on any device.

1 Like

Mobile devices do not have a connected mouse, software or hardware. Input from mobile users is simulated by the position of their touch across a 2D axis and sent as input. Using a mouse on a touch device is not appropriate and shouldn’t be used.

You can very easily extend a ray from ViewportPointToRay without ant mathematical knowledge whatsoever. There’s even a code sample right on the page. A unit ray is simply a ray with a length of one, so you take the generated Ray’s properties, feed it into a new one and extend it’s length by multiplying the unit by the length you want it to reach.

mobile devices do have a simulated mouse, that is why the MouseButton1Down event works on mobile as well as several other of the mouse functions and properties they just work a little differently.

1 Like

Again. They do not. The position of touch across a 2D axis is sent as input. The way Roblox handles input on the engine allows essentially any sent Vector2 position of input to be registered as a point of contact between a user input device and the game.

It is not intended for you to use mouse functions on a mobile device, that’s not only bad practice but that can and will build up into a code smell and potentially inconsistent code while working with mobile. It’s begging for anti-patterns to occur. Telling developers to use mouse functions on mobile is bad advice and you should not be teaching bad practice on the forums. This is a place for learning and learning wrong, unsupported or inappropriate methods of achieving something isn’t proper learning.

Just because it works, doesn’t mean you should use it. The same is applicable for hacks: unconventional methods to resolve an issue, despite the fact that it should not be used in production environments.

1 Like

But if what you saying is true wouldnt the Player:GetMouse() function just return nil then? If it does return a mouse then obviously there must be a simulated mouse.

1 Like

No, it doesn’t have to return nil necessarily, because it returns a mouse in use on the Roblox client on the engine, not a raw user input device from the user’s machine.

You’re missing the complete point of my responses. Just because it works, doesn’t mean you should use it.

I ask that further discussion be taken to DMs as this is starting to derail the thread.

In his original post he asked if Mouse.Target can be used on mobile and I showed him how, if u didnt see in the last line of the post i said he could use billboardgui’s i am not endorsing the incorrect way just showing him what he asked.

1 Like

Yes and that’s fine. It can be used. Doesn’t mean it should be used. That is the entire point of my response. Please read it before replying.