Issue with the mouse's target?

Hey, so I’m having an issue using the Target property of the player’s mouse. It appears that no matter where I point my mouse, it’ll never detect that it’s aiming at a limb of my character. I just print whatever the mouse’ target is, and it seems to just go right through my character.

Also tried setting the TargetFilter to nil, can’t figure out why this might be happening…

Any info appreciated, thanks.

1 Like

Maybe try removing the player’s character and then clicking, and see what the script returns.

1 Like

I just tested it in a new game, with no different result.

Not sure if they ever released a new property somewhere else, but I’m currently using the following code to access the target:

 game.Players.LocalPlayer:GetMouse().Target

This is really annoying…just going to assume it’s a bug with the Roblox engine unless I’m missing something here.

Updated

local localPlayer = game.Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local camera = workspace.CurrentCamera
local lastPlayer
local UIEnabled
local length = 125 -- distance to scan

game["Run Service"].RenderStepped:Connect(function()
local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)

local target = workspace:FindPartOnRay(ray, localPlayer.Character)

if target and target.Parent and target.Parent:FindFirstChild("Humanoid") then
	local playerTarget = game.Players:GetPlayerFromCharacter(target.Parent) -- checks if target is a player
	if playerTarget then --playerTarget if you want the mouse to scan a Player. Change to Target if you want it to scan anything with a Humanoid in it.
			print(target.Name)
--code you want
		end
	end
end)
2 Likes

The Mouse object is very deprecated, but you can use workspace:FindPartOnRay to find a part from the mouse’s UnitRay property:

local RunService = game:GetService("RunService")

local mouse = game:GetService("Players").LocalPlayer:GetMouse()

-- Run this function every frame
RunService.RenderStepped:Connect(function()
	
	-- Save mouse.UnitRay to a variable
	local mouseRay = mouse.UnitRay

	-- Make the ray longer
	local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)

	-- Pass ray to workspace:FindPartOnRay
	local p = workspace:FindPartOnRay(castRay)
	print(p)
end)

The new services that have superceded the Mouse object is UserInputService, as well as ContextActionService to the same extent.

An example of each in practice:

local UserInputService = game:GetService("UserInputService")

local camera = workspace.CurrentCamera

-- Variables used to save the mouse's position on the screen
local x, y = 0, 0

-- Connection to UIS's InputChanged event
UserInputService.InputChanged:Connect(function(input, processed)

	-- Checking for the right UserInputType
	if input.UserInputType == Enum.UserInputType.MouseMovement then

		-- Saving the input's position to x and y
		x, y = input.Position.X, input.Position.Y
	end
end)
local ContextActionService = game:GetService("ContextActionService")

local x, y = 0, 0

-- Binding an action using CAS's BindAction function
ContextActionService:BindAction("raycasting",function(name, state, input)

	-- Checking for the right UserInputState
	if state == Enum.UserInputState.Change then

		-- Saving the input's position to x and y
		x, y = input.Position.X, input.Position.Y
	end

-- Binding the action to the mouse's movement
end,false,Enum.UserInputType.MouseMovement)
-- Run this function every frame
RunService.RenderStepped:Connect(function()

	-- Creating the ray from x and y, passed into camera:ScreenPointToRay
	local mouseRay = camera:ScreenPointToRay(x, y)

	-- Make the ray longer
	local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)

	-- Pass ray to workspace:FindPartOnRay
	local p = workspace:FindPartOnRay(castRay)

	print(p)
end)

Thanks Prince_Duke :+1:

8 Likes

This is false. The mouse object is not deprecated, just the KeyDown and KeyUp functionality of it.

Quote from the wiki:

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

  • The PluginMouse object is still used by plugins accessing the mouse

There are other reasons listed there as well, but the mouse is definitely not deprecated.

4 Likes

Most curious though, I believe the only way to easily access what object the mouse is looking at is with the Target property, which supposedly is not deprecated, which would make sense to there being nothing wrong with it?

I don’t get it. Might also be worth noting this was tested with R16 rigs.

The character is automatically ignored by the mouse, even if you set a TargetFilter. You will need to extend the mouse’s UnitRay to accomplish this using one of the above methods.

5 Likes

Most likely, this is due to the fact that the mouse may be filtering out your own character. Try casting a ray using mouse.UnitRay instead.

1 Like