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)
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)
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.
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
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”)
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.
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)