IsA detects the type but applies the code to not the correct type

Hello, I was using raycasting to try and make a simple gun tool, I wanted to, upon the ray casting check the children of the model or part, if it contains a “Humanoid” and when it does detect it subtracts 10 hp from the Humanoid, but whenever It hits the player character, despite it only checking for “Humanoid” objects it tries to take away the Health from a Body Part.

also sorry if some of this doesn’t make sense I’m pretty tired and barely function.

if Resutlt then
		print("RayCasted")
		local hitObj = Resutlt.Instance
		for i, child in ipairs(hitObj.Parent:GetChildren()) do
			if child:IsA("Humanoid") then
				print("humanoid detected",child:IsA("Humanoid"),child)
				child.Health = hitObj.Health - 10
				break
			end
		end
	end

Can’t you just use:

local hum = hitObj.Parent:FindFirstChildOfClass("Humanoid")
if hum then
  hum:TakeDamage(10)
end

When making gun systems, I use this method ^^

1 Like

You are subtracting the HitObj’s “health” property (which doesn’t exist). You should be using Humanoid:TakeDamage(x)anyway.

1 Like

I never actually knew about the TakeDamage() function, or the FindFirstChildOfClass() thing either, thank you for all the help, it works now