Part Humanoid has negative health!?

Hello developers! Can anybody help me, my “ForceField” humanoid health is going through negative health when using TakeDamage()

I just want it to not be negative AND if the “ForceField” has no health, then it destroys. Please help me I’ve been struggling.

(I’m sorry if it sounds confusing)

CODE:

local humanoid = script.Parent:WaitForChild("ShieldHumanoid")
local gui = script.Parent.Billboard
local label = gui.Label
local bar = gui.Bar
local part = script.Parent
local character = script.Parent.Parent

while wait() do
	label.Text = "SHIELD HP: "..math.floor(part:WaitForChild("ShieldHumanoid").Health).."/"..part:WaitForChild("ShieldHumanoid").MaxHealth
	
	--bar.Size = UDim2.new(0,(part:WaitForChild("ShieldHumanoid").Health / part:WaitForChild("ShieldHumanoid").MaxHealth * 200),1,0)
end

So I experienced an issue like this and uh… I found a way but it maybe a bit in-efficient if you have multiple shield parts. Basically you have to add a script that will check for the HP and if it is lower than 0, set it to 0.

Example code:

local HP = script.Parent.HP.Value -- I suppose you are using an int value
local HHP = script.Parent.Health -- Just incase you are using a humanoid
local LowestHP = 0

while wait(0.5) do -- set hp every 0.5 seconds
  if HP < 0 or HHP < 0 then
      HP = 0
   
      HHP = 0
  end
end)

Ask any questions. : )

Nice! How can I add a function that if it’s at 0, it uses Destroy()

I’m still at noob at scripting on health bars.

Oh, I forgot to delete that line, I’m not really using it.

humanoid.Died:Connect(function()
	local model = humanoid:FindFirstAncestorWhichIsA("Model")
	model:Destroy()
end)

Is “humanoid” a Humanoid instance? If so you can connect a callback to its “Died” event like in the above to destroy the containing model.

Then, you would just add another script that would see if it is equal to 0 and destroy it!

example code:

local Check = script.Parent.HP.Value -- assuming ur using an int value
local CheckH = script.Parent.Health -- just incase ur using a humanoid

while wait(0.5) do

  if Check <= 0 or CheckH <= 0 then
     
     script.parent:Destroy() -- assuming the script is inside the shield part.
   
  end

end)

Tried it out with a print() but it didn’t print and no error.

Btw I’m using a Script and a different name for the Humanoid so Idk if that gonna help.

Already tried that with a print() but no print nor error :frowning_face:

local humanoid = script.Parent:WaitForChild("ShieldHumanoid")

What is this though? Is it a part?

It is a humanoid but different name

humanoid.Died:Connect(function()
	character:Destroy()
end)

humanoid.HealthChanged:Connect(function(health)
	if math.floor(health) <= 100 then
		character:Destroy()
	end
end)

Try either of these, again this will only work providing the rest of the script is functional.

Still not working :frowning_face: Here is my picture
Screenshot (681)

Don’t worry about “Fading” script and if you wondering, yes HealthMain gets disabled and it is also where my error is from.

Yeah, the script is likely dysfunctional then, best of luck.

Super sorry to keep annoying you but I revamped my code:

local humanoid = script.Parent:FindFirstAncestorOfClass("Humanoid")
local gui = script.Parent.Billboard
local label = gui.Label
local bar = gui.Bar
local part = script.Parent
local character = script.Parent.Parent

while wait() do
	label.Text = "SHIELD HP: "..math.floor(humanoid.Health).."/"..humanoid.MaxHealth
end

while wait(0.1) do
	print(humanoid.Health)
end

But now I keep getting this error now:
20:33:43.381 Workspace.axelito123456.ForceFieldShield.Handle.HealthMain:9: attempt to index nil with 'Health' - Server - HealthMain:9

Ancestor means .Parent or .Parent.Parent etc…

If you mean child or the child of the child etc, that is called a descendant… If the humanoid is a child of script.Parent then use script.Parent:FindFirstChildWhichIsA(“Humanoid”) otherwise, if it is a descendant, then use script.Parent:FindFirstDescendant(“humanoid name here”)

(Edit: I FOUND A FIX FOR MY PROBLEM! THANKS TO WHOEVER TRIED TO HELP!)

Oh, well forget all that now my problem is that my health is going negative and I can’t stop it at all :frowning_face:

(Edit: Should I report this as a bug? I swear I cannot stop the health from going negative by using TakeDamage())

I don’t think it’s a bug. You can just use math.max to find out whether 0 or the humanoid’s health is bigger, if the humanoid’s health is bigger then set the humanoid’s health to that, otherwise 0.

humanoid:GetPropertyChangedSignal('Health'):Connect(function()
    humanoid.Health = math.max(humanoid.Health, 0)
end)

Or, alternatively, modify your :TakeDamage function to take whatever is smaller, the humanoid’s health or the damage to take:

local damageToTake = 20
humanoid:TakeDamage(math.min(damageToTake, humanoid.Health)) -- if the humanoid's health is 10, it would take 10 damage, otherwise it would take 20

(omg sorry for the bump, I didn’t realize this post was 10 days old)

3 Likes

This helped me damaging my armor and preventing it to go negative

1 Like