Issue with my raycast module

Alright, this is my first post, so bare with me.

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! :smiley:

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.

1 Like

Alright so uh. When i tried doing a headshot it printed out the “Helmet”, what should i do?

at line 2 you should instead try something like

if raycast.Instance.Name == "Head" or raycast.Instance.Parent.Name == "Head" and raycast.Instance.Parent:FindFirstChild("Humanoid") then

end

Hm. I tried something like that, ill try and do it again.

Still nope. 2022-04-08 21-43-27

if raycast.Instance.Name == "Head" or raycast.Instance.Parent.Name == "Head" then

does it say anything in output?

This time it didn’t even print out anything.

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

if raycast.Instance.Name == "Head" or raycast.Instance.Parent.Name == "Head"  or raycast.Instance.Parent.Parent.Name == "Head" then

Still no..

Nothing in the console? If not can you try getting the Character?

I’ll sound dumb, how would i get the character from a raycast?, first time working with raycasting i suck at it lol

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