Mouse.target not working on npcs

Hi I have this gun that I scripted using mouse.Target however it only works when you aim at the NPCs head
I’m pretty sure the issue is not the NPC since I’ve tried multiple NPCs but maybe it could be?

When the cursor is red that means it has found a target, however you can see that it is white when aiming at different body parts and the mouse.target is actually the baseplate for some reason


Output of mouse.target:
image

Code:

while task.wait(0.03) do
		game.Players.LocalPlayer.PlayerGui.GUI.Crosshair.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
		if currentTarget ~= mouse.Target then
			currentTarget = mouse.Target
			if mouse.Target and mouse.Target:FindFirstAncestorOfClass("Model") then
				if mouse.Target:FindFirstAncestorOfClass("Model"):FindFirstChild("Killer") then
					changeCol(Color3.fromRGB(255, 0, 0))
				else
					changeCol(Color3.fromRGB(255, 255, 255))
				end
			else
				changeCol(Color3.fromRGB(255, 255, 255))
			end
		end
		if script.Parent.Parent.Parent.Parent ~= game.Players.LocalPlayer.Character then
			break
		end
	end
1 Like

Try raycasting instead. Hopefully this will fix your problem.

1 Like

Yes, Roblox actually prefers you don’t use the mouse object anymore because there are other methods that are better

Instead using a loop you can try this approach:

-- You need this on top 
local userInputService = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local runService = game:GetService("RunService")


-- Use RenderStepped for crosshair position
runService.RenderStepped:Connect(function()
	local mouse = userInputService:GetMouseLocation()
	game.Players.LocalPlayer.PlayerGui.GUI.Crosshair.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
end)

-- Connect a function to mouse.Move 
mouse.Move:Connect(function()

	if mouse.Target and mouse.Target:FindFirstAncestorOfClass("Model") then
		if mouse.Target:FindFirstAncestorOfClass("Model"):FindFirstChild("Killer") then
			changeCol(Color3.fromRGB(255, 0, 0))
		else
			changeCol(Color3.fromRGB(255, 255, 255))
		end
	else
		changeCol(Color3.fromRGB(255, 255, 255))
	end

end)
1 Like

Wouldnt it be laggy to fire a raycast every 0.03 seconds?

this seems to have fixed the issue somehow! thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.