Or doesn’t work, it prints “Head”, but “UpperTorso” doesn’t print
-- Mouse.Move:Connect(function()
if Mouse.Target.Name == "UpperTorso" or Mouse.Target.Name == "Head" then
local Char =Mouse.Target
print(Char)
end
end)
Or doesn’t work, it prints “Head”, but “UpperTorso” doesn’t print
-- Mouse.Move:Connect(function()
if Mouse.Target.Name == "UpperTorso" or Mouse.Target.Name == "Head" then
local Char =Mouse.Target
print(Char)
end
end)
Do you use R15 characters or R6 characters? Does your test target wear any big body accessories?
I would recommend checking if Target.Parent got humanoid instead
if Mouse.Target.Parent:FindFirstChild('Humanoid') then
local char = Mouse.Target.Parent
local plr = game:GetService('Players'):GetPlayerFromCharacter(char)
end
@R0mAAn1CH said it already, the accessories are stopping the raycast.
If you are looking for UpperTorso
or Head
specifically, a neat solution is disabling CanQuery
property on character’s accessories.
(Also possible on client - local script in StarterPlayerScripts. That would require a connection for each player, and initial update for all existing characters.)
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
for _,object in ipairs(character:GetChildren()) do
if not object:IsA("Accessory") then continue; end
for _,part in ipairs(object:GetDescendants()) do
if not part:IsA("BasePart") then continue; end
part.CanQuery = false
end
end
end)
end)
local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local target = nil
UIS.InputChanged:Connect(function(input, gameProcessed)
if gameProcessed then return; end
if input.UserInputType == Enum.UserInputType.MouseMovement then
if not mouse.Target or mouse.Target == target then return; end
target = mouse.Target
if target.Name == "UpperTorso" or target.Name == "Head" then
print(target.Name)
end
end
end)
Works like a charm. A small caveat: mouse.Target
can be nil
, and mouse
automatically ignores your own character. To detect your character (too), camera:ScreenPointToRay()
and your own raycast is required. Same applies if you don’t want to use the CanQuery
solution. There are plenty of posts regarding this approach around the Dev Forum.
Technically, the following works well too, but has certain limitations that manual raycasting doesn’t, especially one in particular: filter. This won’t comply well with other scripts relying on mouse.Target.
local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local target = nil
UIS.InputChanged:Connect(function(input, gameProcessed)
if gameProcessed then return; end
if input.UserInputType == Enum.UserInputType.MouseMovement then
if not mouse.Target or mouse.Target == target then return; end
target = mouse.Target
local accessoryAncestor = target:FindFirstAncestorOfClass("Accessory")
if accessoryAncestor then
mouse.TargetFilter = accessoryAncestor
end
if target.Name == "UpperTorso" or target.Name == "Head" then
print(target.Name)
end
end
end)
Does it hit HumanoidRootPart instead of UpperTorso
You can fix it by manually RayCasting with the filter
RP.FilterDescendantsInstances = {Character.Head, Character.UpperTorso}
This also omits needing to check for the name
Take note that you need to change the FilterDescendantsInstances everytime the character responds
Im pretty sure it’s “Upper Torso” and not “UpperTorso”