Attempt to compare number < instance?


function onTouch(hit, bullet)
		if hit.Parent:findFirstChild("Zombie") then
			if hit.Parent:findFirstChild("Zombie").Health > 0 then
end
end
end

I get this error when trying to compare the humanoid health > 0 →

 Workspace.matt1020304050.control:261: attempt to compare number < Instance  -  Client - control:261

Is Zombie a Humanoid? and Is there an Instance called Health in Zombie?

It is because you are trying to access the “Health” variable outside the humanoid.
There is a script called “Health” inside rigs so that is why you are getting that error.

2 Likes

Yes I’m trying to access the health property via script. I don’t recall this ever being an issue in the past? How do I do this without error?

if hit.Parent:FindFirstChild("Zombie"):FindFirstChildOfClass("Humanoid").Health

I don’t get it? “Zombie” in my case is the name of the humanoid.

first of all
use FindFirstChild instead of findFirstChild cuz roblos deprecated the one with camelCase

second
you’re doing Zombie:FindFirstChild("Health")
do Zombie:FindFirstChildOfClass("Humanoid").Health.

‘’’
function onTouch(hit, bullet)
if hit.Parent:findFirstChild(“Zombie”) then
if hit.Parent:findFirstChild(“Zombie”).Humanoid.Health > 0 then
end
end
end
‘’’

I’m not doing Zombie:FindFirstChild(“Health”)

I’m doing
hit.Parent:findFirstChild(“Zombie”)

And then I’m accessing the health property after?

I get the purpose of findfirstchildofclass, but in my case this wont change anything? It’s just another way of writing it.

ok but the Health script shouldn’t be parented to humanoid
it should be parented to the model

This is a script in a gun I’m building. I’m just trying to find an answer to what my error is.

can you do print(type(Zombie))?

can you maybe show what the target the gun detects looks like in explorer?

Hey try to do this instead:

if hit.Parent:findFirstChild(“Zombie”).Health.Value > 0 then

I assume the Health that you are trying to access is a value object.

Can you also try hit.Parent:FindFirstChildOfClass("Humanoid").Health instead?

function onTouch(hit, bullet)
	if hit.Parent.Name == "Zombie" and hit.Parent:FindFirstChild("Humanoid").Health > 0 then
	end
end
function onTouch(hit, bullet)
    local Hit = hit.Parent
    if Hit.Name == "Zombie" then
        local Hum = Hit:FindFirstChild("Humanoid")
        if Hum.Health > 0 then
        end
    end
end
function onTouch(hit, bullet)
	local Hit = hit.Parent
	if Hit.Name == "Zombie" then
		if Hit.Humanoid.Health > 0 then
		end
	end
end
local db = true
function onTouch(hit, bullet)
	if db then
		local Hit = hit.Parent
		if Hit.Name == "Zombie" then db = false
			local Hum = Hit:FindFirstChild("Humanoid")
			if Hum and Hum.Health > 0 then
				Hum.Health -= 10
                task.wait(0.33)
			end	db = true
		end
	end
end

pick one …

I figured out the issue. I was doing

if hit.Parent:findFirstChild("Zombie") then 

And the bullet was hitting a part in the workspace, and the workspace contains a model or humanoid or something named “Zombie”

Zombie is the name of the NPC … you will not find that within hit. It is the hits name. Once you know that is Zombie, you also know Humanoid is within that.

local debounce
local function onTouch(hit, bullet)
 local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if debounce and not (character.Name == "Zombie" or humanoid) then
return
end
debounce = true
humanoid:TakeDamage(10)
task.wait(0.33)
debounce = false
end```

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