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