I just have this question, i have taken a look at things talking about mouse just right now, and i saw that you can know whenever someone clicks with the mouse, but i don’t know how to get the cursor’s current target with UserInputService, just like this would do:
local plr = game.Players.LocalPlayer
local M = plr:GetMouse()
print(M.Target)
There is a degree of overlap between the functionality offered by ContextActionService , UserInputService and the Mouse object.
Mouse, by and large, has been superseded by UserInputService which offers wider additional functionality for interacting with the mouse as well as other input types. For example:
UserInputService supports other inputs such as key presses and mobile inputs whereas Mouse does not
UserInputService includes additional mouse features such as UserInputService.MouseBehavior and UserInputService.MouseDeltaSensitivity
It is still necessary to use Mouse for a number of things. There is a reason Mouse is not deprecated. Not because old stuff uses it (see when they forced FilteringEnabled on old games), but because it has numerous things that no other service, or instance can provide. That being:
Mouse.Target
Mouse.Hit
Mouse.Position (I think there’s two properties for this, I forget.)
So, no, there still is no other way to get the mouse’s target. My apologies.
Yeah, there isn’t a built in method, but if you really didn’t want to use the mouse object and because Mouse.Target is just essentially raycasting, an identical method would be something like:
local UIS = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
local Camera= game.Workspace.CurrentCamera
local MaxLength = 500
local function GetMouseTarget(ignore)
local MousePos = UIS:GetMouseLocation() - GuiService:GetGuiInset()
local unitray = Camera:ScreenPointToRay(MousePos.x, MousePos.y)
local ray = Ray.new(unitray.Origin, unitray.Direction * MaxLength)
return workspace:FindPartOnRay(ray, ignore)
end