Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Best Way to avoid negative health on a Zombie/Humanoid?

Asked by 5 years ago

So I'm trying to make a model appear when a zombie dies to delete it and have it explode the code I am using is this


health = script.Parent:findFirstChild("Humanoid") debounce = false local self = script.Parent:clone() while true do wait(0.1) if (health.Health == 0) then if (debounce == true) then return end debounce = true wait(1) t = game.Lighting.Part:Clone() t.Parent = game.Workspace wait(10) t:remove() local clone = self:clone() clone.Parent = game.Workspace clone:MakeJoints() script.Parent:remove() end end

and I've tried changing it to the negatives I've been getting but that hasn't worked so I want a way to avoid negative health

0
If you are using a Humanoid, you should not have issues with negative values for health because the Humanoid should round all negatives up to 0. alphawolvess 1784 — 5y

2 answers

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago

Use "math.clamp." Math.clamp will set the first argument to the third argument if it's greater than the third argument, and it will set the first argument to the second argument if it's below the second argument. Otherwise, it will return the first argument. This will also work with decimals.

Usage: math.clamp(valueToClamp, minValue, maxValue)

How to use:

print(math.clamp(5, 0, 36)) --returns 5
print(math.clamp(-5, 0, 36)) --returns 0
print(math.clamp(37, 0, 36)) --returns 36

In your case, you should set the maxValue to the zombie's max health.

Ad
Log in to vote
0
Answered by
NIMI5Q -2
5 years ago
Edited 5 years ago
local sign = math.sign(health.Health)
if sign == -1 then health.Health = 0 
elseif sign == 0 then -- code
else --[[ This if the Humanoid Health has not died yet. --]]
-- code
end

-- This will get your Health and return -1 if it exits natural order.
-- ...and 0 for neutral, 1 for natural.
0
Updated local, and I would put it into a function. NIMI5Q -2 — 5y
0
okay. ill wait to see other solutions though but thanks ill probally end up using it ILoveMemes9005 16 — 5y
0
Dude what was wrong with my post? NIMI5Q -2 — 5y
0
nothing ILoveMemes9005 16 — 5y
View all comments (2 more)
0
its just complecated ILoveMemes9005 16 — 5y
0
but i used both of your guys answers on different projects ILoveMemes9005 16 — 5y

Answer this question