Mouse.Target not registering character parts

Hey! My aim of this script is to get the TextLabel to show the name of what part the player has clicked on. This works for all parts, except from characters, which is crucial to this feature of the game. However, for some reason, when the mouse is clicked, it doesn’t register that it’s clicked a part.

Video example:

Code:

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

Mouse.Button1Down:Connect(function()
	local Target = Mouse.Target
	if Target then
		LocalPlayer.PlayerGui.EnliteUI.TextLabel.Text = Target.Name
	end
end)

If anyone has any solutions, please let me know! Thank you.

any errors?

There are no errors. (charsss)

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

Mouse.Button1Down:Connect(function()
	local Target = Mouse.Target
	LocalPlayer.PlayerGui.EnliteUI.TextLabel.Text = tostring(Target)
end)

I tried it, but it made no difference. No errors.

https://developer.roblox.com/en-us/api-reference/property/Mouse/TargetFilter

Mouse filters our character automatically. Set the filter to nil.

1 Like

Setting the filter to nil won’t work, the character’s descendants are automatically filtered by the legacy mouse object.

Here’s a local script which will allow you to target the local player’s character’s descendants with the legacy mouse object.

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist

mouse.Move:Connect(function()
	local raycastResult = workspace:Raycast(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 250, raycastParams)
	
	if raycastResult then
		local raycastHit = raycastResult.Instance
		if raycastHit then
			local raycastModel = raycastHit:FindFirstAncestorOfClass("Model")
			if raycastModel then
				print(raycastModel.Name)
			end
		end
	end
end)

This code snippet will print the local player’s character’s name whenever the mouse’s cursor is hovered over the local player’s character.

1 Like

Where do I place this local script?