Help with healthbar maths

I’m trying to create a health bar for bosses in my game, but not as simple as just showing their ‘Health’ divided by their ‘MaxHealth’.

Here is a graphic of what I’m trying to achieve:

In the graphic, the top bar’s code for its size would be:

UDim2.new(EnemyHumanoid.Health/EnemyHumanoid.MaxHealth, 0, 1, 0)

For the bottom bar, which is what I’m trying to achieve, I’m not really sure how I would go about scripting it. Basically, it will show how much health the boss has before it will lose a life, then once it reaches that number it will be full size until more damage is taken. (unless the boss has 0 lives, in which case the GUI dissapears)

All I’ve got so far is this:

local Lives, MaxLives = Boss:GetAttribute(“Lives”), Boss:GetAttribute(“MaxLives”)

If anyone is able to help with this, I would appreciate it very much!

3 Likes

I think I may get what you mean but could you just us what you want “into this” to look like with even less lives or is it the same?

1 Like

The bottom bar shows how much health the boss has before it loses that life. The amount of health for each life is the boss’s MaxHealth divided by how many lives there are.

Do you mean like the left is you and the right is the other person?

So basically the boss just has 4 normal health bars? :joy:

Yes, I’m going to make it so when the boss is block broken you can do a takedown move and it will lose one of its lives. (depending on how many lives the boss has is how much damage the takedown will do)

1 Like

Try something like this just to get an idea you can just use this as like boilerplate, (im guessing you know enough to do the UI related stuff)

local Boss = {}

-- Constants
local NUM_BARS = 4
local MAX_HEALTH = 100

Boss.healthBars = {}

function Boss:Init()
    for i = 1, NUM_BARS do
        self.healthBars[i] = MAX_HEALTH
    end
end

function Boss:Damage(amount)
    for i = 1, NUM_BARS do
        if self.healthBars[i] > 0 then
            self.healthBars[i] = math.max(self.healthBars[i] - amount, 0)
            break  -- Exit the loop after damaging one health bar
        end
    end
end

function Boss:IsDefeated()
    for i = 1, NUM_BARS do
        if self.healthBars[i] > 0 then
            return false
        end
    end
    return true
end

Boss:Init()
Boss:Damage(25)
print(Boss.healthBars)  -- Output: { 75, 100, 100, 100 }
print(Boss:IsDefeated())  -- Output: false
2 Likes

Thank you for this! I used your logic with a little tweaking and it works perfectly.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.