Health divided by MaxHealth on healthbar script, help

So, I created a healthbar gui for my monster and I configure its health into 500 MaxHealth and 500 Health but, now, my problem is when I’m trying to get the scale of healthbar into 1 the value becomes 5.

This is what I’m talking about:

local hDivide = boss.Health/boss.MaxHealth
script.Parent:TweenSize(UDim2.new(hDivide, 0, 1, 0), "InOut", "Quad", interval)

When I’m trying to print the value of Divide the output was 5.

How do I fix this? It should be 1 right?
500/500 = 1

(Sorry, Im new in programming)

Try doing something like this:

local currentHealth = 100;
local totalHealth = 100;
local size = (1/totalHealth) * currentHealth

The math is pretty basic,
we’re diving the frame’s largest size ( 1 ) by the total health, and then multiplying that by the current health

A good way to look at this:

If I have a pie that I split into 6 pieces, and I eat 3 pieces of the pie, then I only have 3 left or 50%

So if I have a frame with the width 1, and I divide 1 into 100 pieces, and then multiply that by 50, the frame will be at 50% or 0.5

Still prints 5 in the output.

30 Charssssss

If you want to get percentage, you have to multiply the result by 100.

But your use should work just fine, how you sent it. Are you sure you’re using Scale and not Offset with the Frame? In the script – you’re setting Scale, make sure to check it.

This is the idea: The frame’s size is 1, the Boss health is 100 and the Boss max health is 200.

The divide should be something like this:

Boss.MaxHealth - Boss.Health

Yes, I use the scale of the frame.

That’s not mathematically possible.
1 / 500 * 500 is 1…
so if the monster is at full health the frame scale should be 1… likewise
1 / 500 * 250 is 0.5…
so if the monster is at 250 health, the frame scale should be 0.5

What exactly prints 5? 500/500 should NOT print 5.

I create a variable that holds the formula of @Crowdsource given.

local hDivide  = (1/bossMaxHealth) * bossHealth
print(hDivide) --[Output in-game: 5] [Output in-studio: 1]

If it’s just a frame then i’d do:

frame.Size = UDim2.new(CurrentHealth/MaxHealth, 0, 1, 0)

This works for me when i use it in Experience values.
Also my frame is scaled as: {1, 0},{1, 0} for reference.

1 Like

So, I make a LocalScript and put it in the StarterGui.

Localscript:

while wait(5) do
	local monster = game:GetService("Workspace"):WaitForChild("Killers"):WaitForChild("Jaguar")
	local boss = monster:WaitForChild("Zombie")
	local bossHealth = boss.Health
	local bossMaxHealth = boss.MaxHealth

	print("Formula 1: "..(1/bossMaxHealth) * bossHealth)
	print("Formula 2: "..bossHealth/bossMaxHealth)
end

Output:

1 Like

Could you also print the bossHealth and the bossMaxHealth?

1 Like

I fixed it now, by looking up on the script that handles the path finding of the npc.

I’ve notice there is an assignment for MaxHealth = 100, that’t why 500/100 = 5 instead of 500/500.

My bad guys.

Btw all of the formula are working.

Thanks again for the people who helped me.

Sorry.

1 Like