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

How can I tell how much percent of health a humanoid has lost?

I'm not sure how to actually do this, I need this for a battle injury script, any help would be appreciated.

script -

Humanoid.HealthChanged:Connect(function()
	if Humanoid.Health < "the percent" then
		
		print("lost " .. .."% of health")
		
	end
	
end)

1 comment

0

You can just get the current users health and users MaxHealth

3 answers

✓ Accepted answer
local hp = 500 -- current humanoid hp
local maxhp = 1000
Humanoid.Health.Changed:Connect(function()
	hp =Humanoid.Health
	print("%".. tostring(hp/maxhp *100))
end)

-- example

current hp 500, max hp 2000

print("%".. tostring(hp/maxhp *100)) outputs 25%

You get the users humanoid health and max health.

Humanoid.HealthChanged:Connect(function()
	print(humanoid.MaxHealth - humanoid.Health)
end)

2 comments

0

i need to know how much percent of damage from the maxhealth they took, not how much damage they took.

0

Then just convert the MaxHealth - Health to a percentage.

You can get the amount of hp changed by simply storing the current hp of a player until it changes and doing some math.

repeat wait() until game.Players.LocalPlayer.Character
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = humanoid.Health
humanoid.HealthChanged:Connect(function(health)
	local Difference = math.abs(currentHealth - health) -- We want the absolute number so we dont get any negative results. This gets the number difference of the hp lost (100hp - 25 taken = 75 Difference)
	local DifferencePercentMax = ((Difference/humanoid.MaxHealth)*100) -- Gets the percentage of the difference, based on players max hp  
	local DifferencePerecentCurrent = ((Difference/currentHealth)*100) -- Gets the percentage of the difference, based on how much hp the player had
	print(humanoid.Name.. " lost ".. Difference.. " hp!")
	print(humanoid.Name.. " lost ".. DifferencePercentMax.. "% of their total hp!")
	print(humanoid.Name.. " lost ".. DifferencePerecentCurrent.. "% of their current hp!")
	currentHealth = health
end)

	-- So if your player takes 25 damage while he has full hp of 100 then your prints would look like so
	--print(humanoid.Name.. " lost ".. Difference.. " hp!") -- "WizyTheNinja lost 25 hp!"
	--print(humanoid.Name.. " lost ".. DifferencePercentMax.. "% of their total hp!") -- "WizyTheNinja lost 25% of their total hp!"
	--print(humanoid.Name.. " lost ".. DifferencePerecentCurrent.. "% of their current hp!") -- "WizyTheNinja lost 25% of their current hp!"
--If you take an additional 15 hp the last print would look like this
	--print(humanoid.Name.. " lost ".. DifferencePerecentCurrent.. "% of their current hp!") -- "WizyTheNinja lost 20% of their current hp!"

--I recommend using math.floor() to display the results, so you dont get results like .3333333
--print(humanoid.Name.. " lost ".. math.floor(DifferencePercentMax).. "% of their total hp!") -- "WizyTheNinja lost 25% of their total hp!"
-- If you had 75 hp and took 25 hp your difference would be 33.33333333% difference so instead of printing "WizyTheNinja lost %33.33333333% of their total hp!" it would print "lost %33 of their totalHP")
--Math.floor just cuts off all the decimals of a number
-- math.floor(1.555) = 1
-- math.floor(1.555*10)/10 =  1.5
-- math.floor(1.555*100)/100 = 1.55
-- math.floor(1.555*1000)/1000 = 1.555
--etc...

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