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
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