NaN (not a number) health problem

Hello devs, i wonder if its possible to make anti NaN health, because here’s a way to make enemy in my game have inf health which makes him impossible to kill. Script i made:
myHuman.HealthChanged:Connect(function(hp)
if myHuman.Health==“nan” or myHuman.Health==“inf” then
myHuman.Health=myHuman.MaxHealth
end
if myHuman.Health>myHuman.MaxHealth then
myHuman.Health=myHuman.MaxHealth
end
if myHuman.Health<0 then
myHuman.Health=0
end
end)

its not working sadly.

check if a number is nan by comparing it

1 Like

And about the infinite health… you can do

if value >= math.huge then
    -- do something
end

Some background: NaN is weird. Nothing is equal to NaN, not even NaN itself. In this case, as suggested by the first reply, you’d need to compare the health with itself to check for NaN. It could look like this:

if myHuman.Health ~= myHuman.Health then

Additionally, you don’t need to compare with infinity because this is already covered by the condition myHuman.Health > myHuman.MaxHealth.

faster isn’t always better, take your time when replying