Huh? Code error where there hasn't been one before

This line of code errored for the first time ever.

if result then
	local hitPart = result.Instance
	local character = hitPart:FindFirstAncestorWhichIsA("Model")
	if character and character:FindFirstChild("Humanoid") then
		if character.Humanoid.Health > 0 then -- this line errored

The output said:
attempt to compare number < Instance

It looks like the error is coming from trying to compare a number and an object of type Instance. The comparison operator ‘>’ is not defined for comparing a number with an instance.

To fix this error, you may need to modify the code to make sure you are doing the correct comparison between the number and instance. For example, you could try casting the Health property to a number before doing the comparison, like this:

if character and character:FindFirstChild("Humanoid") then
	if character.Humanoid.Health.Value > 0 then

This should make sure that you are comparing two numbers, and not trying to compare a number with an instance.

Thanks.

I tried your suggestion.

It gave me a new error:

Value is not a valid member of Script "Workspace.Humanoid.Health"

Why would it allow Workspace to be the “character” since Workspace does not have a “Humanoid”.

Maybe you have instance named Health in the Humanoid?

I rewrote it without the hitPart:FindFirstAncestorWhichIsA("Model")

I don’t know where I got that line from anyway.

if result then
	local hitPart = result.Instance
	if hitPart.Parent:FindFirstChild("Humanoid") then
		character = hitPart.Parent
		if character.Humanoid.Health > 0 then

Now it works fine.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.