Get hit.Parent to include accessories from the character

if hit:IsDescendantOf(character) then return end

local HitCharacter = hit.Parent
if not HitCharacter then return end

print(HitCharacter)

local TargetHumanoid = HitCharacter:FindFirstChild('Humanoid')
if not TargetHumanoid and not TargetHumanoid.Health > 0 then return end

local TargetPlayer = Players:GetPlayerFromCharacter(HitCharacter)
if not TargetPlayer then return end

The problem I am having is if HitCharacter is an accessory (so hit would be the part of that accessory and thus hit.Parent being the accessory itself)

How can I get it to just check if the Hit.Parent is part of the character either way, as right now, TargetHumanoid line errors out, as Humanoid is not a part of an accessory

1 Like

Perhaps you could also add a check for hit.Parent.Parent?

This should error if no humanoid is found, did you mean to put or instead of and?

Something like this would work.

local hitPlayer = Players:GetPlayerFromCharacter(hit.Parent) or Players:GetPlayerFromCharacter(hit.Parent.Parent)

If you want it to work with non players,

local hitHumanoid
local char = hit
while char.Parent do
    local h = char:FindFirstChildWhichIsA"Humanoid"
    if h then
        hitHumanoid = h
        break
    else
        char = char.Parent
    end
end

you could ignore the accessories unless you need them to be able to be touched

local HitCharacter = hit.Parent
if not HitCharacter then return end
if HitCharacter:IsA("Accessory") then return end

if they need to be touched then @Halalaluyafail3’s example would do the job

Problem with this is if it does hit an accessory then it would do nothing.

As for @Halalaluyafail3 example, seems like a lot of extra code for something that seems like it could fit into one line?

Yeah, I assume you’re using this on non player characters as well

local HitCharacter = hit.Parent
if not HitCharacter then return end
if HitCharacter:IsA("Accessory") then HitCharacter = HitCharacter.Parent end
2 Likes