# How can I use a fraction of health's as a separate value

I don’t know if Im explaining myself correctly as I didn’t even knew how to even research what I was trying to accomplish so I will just explain witha diagram I made

I want to have a screen “Hurt Effect” that turns more redish as your health decreases, heres the issue I dont want it to adjust to the current health since if my health is 50 it will make the screen hurt effect show (a redish kinda screen) so i only want it to start turning redish when health is below 20% and from there turn even more redish, but I have no idea how to do this

I cant really explain msyelf I hope the diagrams explain how im trying to convert a value’s fraction to a separate value with its own percentage that drops respectively to other value’s porcentage, if anyone can help me it would be greatly appreciated

``````Stuff.Value = Humanoid.Health / 5 -- 20% of the health will be used
``````

but since i see you’re trying to make that type of mechanic where the enemy’s extra health is their shield, you can make the ZIndex of the additional health larger than the main or preceding health and have the green bar’s size take the entire length of the frame

then make the red also have the same scripting of the green health as for the resizing effect on changed health, you can also make it so for the color reddishing

``````local originalColor = Color3.fromRGB(255, 255, 255)
local redColor = Color3.fromRGB(255, 0, 0)

game.Workspace.Part.Color = game.Workspace.Part.Color:Lerp(originalColor, redColor, health)
``````

I think you overcomplicated this and I’m puzzled as well. Is it a health bar you want? You just want to get the health value?

This doesnt works as I am not trying to make an extra health mechanic or a healthbar color, What I am trying to do is make the screen redish after your health drops lower than 20%

Think of the 20% fraction as a whole 100% separate from the max health, as your health decreases this fraction should decrease in its own porcentage what I was trying to explain with the diagram was the whole health being 100% but when your health drops to 10% theorically the 20% fraction should be 50%, I am trying to achieve a system that gets the percentage of the 20% fraction automatically thats also able to reflect the percentage on the redish effect imagelabel transparency

I wanna get fractions on porcentage.

If you want to determine if a fraction of the reduced health is reduced, you can just do `healthFraction = (health / fraction) / maxHealth`
I actually don’t know what I just said, but this is something.

Let me go test your formula and see if it works

If you want a decimal percentage of 20 you could just do this:

``````for i=20,0,-1 do
print(i/20)
end
``````

If you want a formula that you could apply as an inverse scale (lower = more red), you can use this as a multiplier on an RGB value:

``````for i=0,20 do
print(math.sqrt((i-20)^2)/20)
end
``````

Edit: I’m now very confused as to what you want. If you just want an algorithm that makes 20 the denominator of a fraction that’s pretty easy:

``````for i=20,0,-1 do
print((i/20)*100)
end
``````

It did not worked, it printed 0.01, when I set my health to 20 and max health was 100 if it achieved what I wanted it would have printed 100

what does this formula does exactly?, also I think scaling was probably the wording I was trying to find not sure, if this does what I wanna achieve then in theory if I set my health to 20 and max health as 100, then it should give me a value of 100 right? and if I set my health to 5 it should give me a value of 25?

Are you trying to get `HurtEffect`?

To get the number of health missing: `hum.MaxHealth - hum.Health`

To get the percentage of health missing: `1 - (hum.Health/hum.MaxHealth)` (ranges from zero to one [e.g. 0.84 = 84%])

to test that out:

``````print('Oh no! The player is missing ' .. (1 - (hum.Health/hum.MaxHealth) * 100 .. '% of their health!')
``````

Alright, so you want to do this?
`healthFraction = health / maxHealth`
and
`healthFraction2 = maxHealth - healthFraction`?

No it doesnt seems like this formula should work

No, this is giving me the porcentage of the whole health, I wanna get the porcentage of the 20% fraction. as if it was scaled to 100%

Can you just do something like

``````local transparency = 0 -- default transparency

local health = hum.Health

if health <= 20 then

transparency = (hum.Health/20)

end

blahblahblah.Transparency = transparency
``````
1 Like

I have implemented your formula into my code and now I will test it

Your formula worked, although Im not sure why which is what I will be researching it worked, Thank you I will now mark you as solution as it seems to properly make the GUI’s hurt effect scale in the fraction’s porcentage

Health set to 10 MAX health 100

I also tested it with other things, I think I will just add tweening to it and color correction and it should be ready

`ImageLabel.Transparency = if Health > 20 then 0 else Health / 20`