How would I check if a player is long pressing a part with their finger on a mobile device?

Say I used the TouchLongPress event. How would I check if the user is pressing on a Part? Or is it not able to see things in the 3D workspace?

You should be able to use it together with Mouse.Target to achieve this

Edit: The Mouse object is deprecated apparently so you’ll have to cast a ray from the location of the touch.

local UserInputService = game:GetService("UserInputService")

local camera = workspace.CurrentCamera
local length = 500

UserInputService.TouchLongPress:Connect(function(touchPositions, state, gameProcessedEvent)
  -- The game engine internally observed and acted on it so we should not
  if gameProcessedEvent then return end
  -- Get touch location of finger 1 on screen
  local touchLocation = touchPositions[1]
  -- Create Unit Ray(A ray with a 1 stud length) from touch location
  local unitRay = camera:ScreenPointToRay(touchLocation.X, touchLocation.Y)
  -- Create a new longer ray
  local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
  local raycast = workspace:Raycast(ray)
  -- The part pressed
  local target = raycast.Instance
end)
1 Like

Oh, alright. I’ll go try that.

Sorry for the really late response.

I tried the Mouse.Target thing and it was successful.

And yes, I’m aware that the Mouse object is deprecated. I don’t really mind using it though. Anyways, thanks.

1 Like