How do i know the mouse's target using UserInputService

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)

You can’t. Simply use:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local Position = Mouse.Hit
local Target = Mouse.Target

This would have to be run in a local script, and you’d have to update Position and Target as needed. But there is no other way.

1 Like

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

As said in this page: Mouse | Documentation - Roblox Creator Hub

Tho it also says:

In most cases developers are advised to use the new UserInputService . However the Mouse object remains supported for a number of reasons.

  • Mouse existed long before UserInputService and a large number of places and gear items are dependent on it
  • The PluginMouse object is still used by plugins accessing the mouse
  • The mouse object is embedded into Tool s and is easier to pick up for new developers

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.

4 Likes

Aight, thank you, i will need to use Mouse.Target for now, if roblox doesn’t does an update…

1 Like

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
10 Likes

I mean can’t you technically (not saying this is the best solution lol) use raycasts and get the first part it collides with?

That’s exactly what the reply right above yours does.