Figuring out what the mouse is highlighting (and where) using UserInputService

The legacy mouse object calculates a Ray every time it moves, which means it also exports members like Hit, Origin, and Target.

UserInputService does not export thiese methods. however I’ve heard from several places that this Mouse behaviour is pretty easy to port over, so how? I only want it to give me a position if a part is being selected (so if the mouse is poiting to the sky, it should be nil)

And to reiterate, I do not want to use Mouse here.

Use GetMouseLocation in conjunction with ViewportPointToRay to achieve this effect:

local function target()
    local mouseLocation = UserInputService:GetMouseLocation()
    local raycastResult = Camera:ViewportPointToRay(
    return raycastResult and raycastResult.Position