I’m trying to get a certain name of a part through Raycasting(ray.Instance:), tried using FindFirstChildOfClass, didnt work. tried FindFirstAncestorWhichIsA, Nope still didn’t work.
Heres my a little part of my code below, any help would be appreciated!
if raycast and tookDMG == false then
if raycast.Instance:FindFirstAncestorWhichIsA("Head") then
game.ReplicatedStorage.remotes.damage:FireServer(hum, headDamage)
tookDMG = true
bullet:Destroy()
elseif raycast.Instance.Name == "Torso" or raycast.Instance.Name == "HumanoidRootPart" then
game.ReplicatedStorage.remotes.damage:FireServer(hum, torsoDamage)
tookDMG = true
bullet:Destroy()
elseif raycast.Instance.Name == "Left Arm" or raycast.Instance.Name == "Right Arm" or raycast.Instance.Name == "Left Leg" or raycast.Instance.Name == "Right Leg" then
game.ReplicatedStorage.remotes.damage:FireServer(hum, limbDamage)
tookDMG = true
bullet:Destroy()
end
end
If the helmets aren’t parented to the Head then they don’t have an ancestor named Head. If you really want to debug this closely so you can find out what names or paths you need to use to detect headshots, print the instance’s full name.
if raycast and tookDMG == false then
print("Hit object: " .. raycast.Instance:GetFullName())
-- Rest of your code
On another note, Head is not a class so it would fail IsA. IsA checks for classes and inheritance not for names. Head is a Part named Head.
try adding a third check and printing out all 3 checks
if raycast.Instance.Name == "Head" or raycast.Instance.Parent.Name == "Head" or raycast.Instance.Parent.Parent.Name == "Head" then print(raycast.Instance.Name) print(raycast.Instance.Parent.Name) print(raycast.Instance.Parent.Parent.Name) end
okay first things first, is the Helmet parented to the character? if it is then you could try:
if raycast.Instance.Parent:FindFirstChild("Humanoid") or raycast.Instance.Parent.Parent:FindFirstChild("Humanoid") then
local HitCharacter = nil
if raycast.Instance.Parent:FindFirstChild("Humanoid") then
HitCharacter = raycast.Instance.Parent
end
if raycast.Instance.Parent.Parent:FindFirstChild("Humanoid") then
HitCharacter = raycast.Instance.Parent.Parent
end
end
You Could then do:
if raycast.Instance:IsADecendantOf(HitCharacter.Head) then
end
that also got me thinking, is the helmet even a child of the head
alright so it works i guess. what bothers me its that whenever the bullet touches something like a part it prints out
ReplicatedStorage.modules.projectile:22: attempt to index nil with 'FindFirstChild’
if raycast and tookDMG == false then
print("Hit object: " .. raycast.Instance:GetFullName())
local hum = raycast.Instance.Parent:FindFirstChild("Humanoid") or raycast.Instance.Parent.Parent:FindFirstChild("Humanoid") or raycast.Instance.Parent.Parent.Parent:FindFirstChild("Humanoid")
--^^ error here
Alright i think i did it. i used “FindFirstAncestor” , make sure you have the “thing”(in this case is my helmet) parented to the head, and also make sure its not a model.
if raycast then
local hum = raycast.Instance.Parent:FindFirstChild("Humanoid") or raycast.Instance.Parent.Parent:FindFirstChild("Humanoid")
if hum then
if raycast.Instance:FindFirstAncestor("Head") then
--code
end
end
end