Help with a custom oxygen bar

So basically my idea is to make an oxygen bar but the catch is that it’s not just a simple frame bar, but in a form of 3 bubbles. The problem is that I’ve no idea how to script it so it’ll make each bubble disappear progressively while the oxygen goes down. If you’ve got any idea of the best method or any good alternatives I’ll be grateful. Thanks in advance.

Here’s a quick screenshot of how the bubble ui looks like:
image_2023-02-11_063304449

Alright so first make a value for the oxygen.

Let’s call this

local ox = 100

Now we need to know what the maximum oxygen is:

local maxOx = 100

Now we will need to get an array with the bubbles inside of them. Either use :GetChildren() or list them like so:

local bubbles = {
   [1] = script.Parent:WaitForChild("Bubble1"), -- fix all the paths
   [2] = script.Parent:WaitForChild("Bubble2"),
   [3] = script.Parent:WaitForChild("Bubble3"),
}

Now Bubble1 will first expire and then the rest and so on.
Now let’s make a quick plan so we know what will happen:
If we only had one bubble we would use:

Bubble.Transparency = 1 - ox/maxOx

Now for 2 bubbles

Bubble2.Transparency = 1 - (ox - maxOx/2 * 1)/(maxOx/2)
Bubble1.Transparency = 1 - (ox - maxOx/2 * 0)/(maxOx/2)

Basically the number 2 stands for the amount of bubbles, maxOx/2 is the Oxygen every bubble is worth.

To make this work for any script do the following every time you want to update the oxygen:

local amnt = #bubbles -- how many bubbles we got
local oxPerBubble = maxOx/amnt -- maxOx per bubble

-- Use this every time your oxygen changes:
for i, bubble in pairs(bubbles)
   bubble.Transparency = 1 - (ox - oxPerBubble*(i-1))/oxPerBubble
end
1 Like

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