Need help with using percentages correctly!

The title I used is kinda vague so let me re-iterate. I need help creating a script that’ll modify a value based on the amount of damage a player takes. For example, if the player takes 50% of his MaxHealth, he’ll get X amount of a value. The player will get more of said value the more damage he takes(and less of said value the less damage he’s taken.

For my script, I need it to add a certain amount onto a value when the player takes damage. Max amount that can be added to the bar is 1000.

For example: Player loses 20% of his max HP, gains 25 “bar” (amount added to the bar).

All help is appreciated.

What exactly do you mean? do you mean like numbers?

local maxheath = 100
local percentloss = 15

maxhealth -= (maxhealth*(percentloss / 100))
--[[ Formula
ChangedValue x (Percentage / 100 )
1 Like

The following code should be placed in StarterCharacterScripts.

local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local health = humanoid.Health
local bar = 0
local maxAmount = 1000
humanoid.HealthChanged:Connect(function(newHealth)
     local damage = health - newHealth
     health = newHealth
     if damage < 0 then return end
     bar += (damage/humanoid.MaxHealth)*maxAmount
end)

Feel free to change bar into an IntValue instead of a variable.

2 Likes

The code you provided doesn’t work.

Actually it did work, I just had to do some edits. I’ll just mark it as the solution.