Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

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.

2 answers

✓ Accepted answer

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.

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.

6 comments

0

Updated local, and I would put it into a function.

0

okay. ill wait to see other solutions though but thanks ill probally end up using it

0

Dude what was wrong with my post?

0

nothing

0

its just complecated

0

but i used both of your guys answers on different projects

This discussion is preserved as it was. New answers, comments, and votes are closed.