Project 566: Custom Health Bar

9. Test and adjust

Test out how the health bar is working now. Does anything seem strange about it?
This challenge doesn't have a starting point

Steps

1. Why is the health bar super long?

The health bar is super long because we set the X Scale of the health bar to be equal to the player’s current health. 

When the game starts, the player health is at 100 - which means the health bar is size 100.

The biggest the health bar should ever be, to make sure it matches the parent scale, is just 1! 

Let’s say the player loses some health, and now only has 75 points of health. The health bar would still be size 75, which is enormous. 

We’ll need to make the health bar a decimal. The next step will show you how. 

2. Create an equation for a different health variable

How would you turn the number 100 into the number 1? Or the number 75 into the number .75?

You could divide the first number by 100 to get it to be a decimal.
 
In the case of our code, we’d want to divide the player’s actual health by the player’s max health (which is 100.)

So if the player has full health, 100 divided by 100 becomes 1. If the player has 75 health, 75 divided by 100 becomes .75

Put this inside the HealthUpdate function.
humanoid.Health/humanoid.MaxHealth

3. Initialize the new health variable

To keep track of this new decimal number, we’ll make it a variable. We can call it “currentHealth” or “decimalHealth” or something else that makes sense to you. 
local currentHealth = humanoid.Health/humanoid.MaxHealth

4. Use the new health variable

Go back into your UDim2 coordinate. What parameter would you want to replace with your new health variable?
healthBar.Size = UDim2.new(currentHealth, 0, 1, 0)

5. Test again!

Test this again and your health bar should be working as expected!