Help finding the players character from a specific part of the character?

Im trying to make it so when the gun hits a player it will do damage which I have successfully done. But when I hit like an accessory It wont work because of the way the script is. Its set so when the raycast his a player is prints its instance name and then from that I find the parent then the humanoid. I want to do something like findfirstparent(The players character) then do something like .Humanoid Can someone help me out on this?

Ill show my code and what happens when you shoot someone
image

Use FindFirstAncestorOfClass

That way you can find the first ancestor that’s a model and then check if that is the character. It’s how I usually handle that.

1 Like
local hitPart = ray.Instance
if hitPart then
	local hitModel = hitPart:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitHuman = hitModel:FindFirstChildOfClass("Humanoid")
		if hitHuman then
			hitHuman:TakeDamage(hitHuman.Health)
		end
	end
end

ray.Instance is nil if the casted ray does not intercept a BasePart instance.

1 Like