What is the most effective way to do a stamina bar this way

So im making a source engine replica and im tryna make the same stamina bar as hl2
reference:
​​
Captura de pantalla 2024-09-03 154129

im trying to not make each square a ui but i cant really find a better solution than that and i dont think inside code would work so far

2 Likes

Maybe set a value for stamina and reduce it when running, then divide the bar size by the stamina left and subtract that size from the bar, and add it when the stamina comes back.

yeah but that wouldnt have thoose squares im searching for

It’s probably better to just make each square a ui. If you made it an image that would be even harder, Im not sure then.

I ended up doing each square a ui thogh idk how i will integrate the stamina with this since i have to make em look darker when u waste stamina here is how the ui ended up though
image

Like I said in the last comment, I think you can probably set a value to your amount of squares 2x, then each time the value drops by 2, subtract one square, or make it darker. I made a quick local script to understand what Im doing:

local StaminaAmount = #script.Parent.Squares:GetChildren() * 2 + 2 -- The square amount

local UIS = game:GetService("UserInputService")

local LightColor = Color3.new(1, 0.796063, 0) -- replace with your colour
local DarkColor = Color3.new(0.669337, 0.571878, 0.190845) --replace with your color

local IsRunning = false

UIS.InputBegan:Connect(function(input, processed)
	if processed then return end
	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		IsRunning = true
		while IsRunning == true do
			task.wait(0.5)
			StaminaAmount -= 1
			print(StaminaAmount)
			if StaminaAmount <= 0 then
				break
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, processed)
	if processed then return end
	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		IsRunning = false
		while IsRunning == false do
			task.wait(0.5)
			StaminaAmount += 1
			print(StaminaAmount)
			if StaminaAmount >= #script.Parent.Squares:GetChildren() * 2 + 2 then
				break
			end
		end
	end
end)

while task.wait() do
	for i,v in pairs(script.Parent.Squares:GetChildren()) do
		if StaminaAmount > i * 2 then
			v.BackgroundColor3 = LightColor
		else
			v.BackgroundColor3 = DarkColor
		end
	end
end

This could be more optimised but that wasn’t my goal here.

2 Likes

i amnot 100% sure if this is more optimized
make 1 image that have those rectangles and make the gap between them(transparant)
you will need to save it as a RGBA

then use it in roblox studio and add 1 UiGradient
then change its color by changing the UiGradient Color Sequence and if you want it to fade away when stamina is low then change the Transparency sequence (but that will need alot of scripting)

So i tried doing what you said buit they arent changing color so far

it does change but after a long while and in the incorrect order and when they fully change color they change at once

I used frames, and I put them in a folder called squares and named them 1 through 6, I think it has something to so with the order.
Screenshot 2024-09-03 at 7.58.43 PM

Imma try it rq tysm for helping

So i use frames too and they are inside a frame i renamed em as u did and still they arent ordered so i dont rlly know what to do

Mines are ordered with the position as well, if frame 1 is in the middle of the squares, its of course going to go first, so order them, the first one going on the very left and the last one on the very right.

They are ordered like that i believe lemme do a ss

ezgif.com-video-to-gif-converter

Heres how mine is laid out, Sorry for the lag, I had to make it a gif for the size restrictions.

Imma try renaming them again ig

Its the positions probably, mine is also laid out in order.

Wdym by laid out in order like what i did was create one then duplicate i positioned it change its name and same with the others is that maybe the issue?

I did the same, in the gif, I press on each frame to show what number one is on the very right and number six is on the very left and they are named like so, if its probably doing that because the for loop goes from top to bottom as well, so the first frame has to be the very right one and the last should be the very left.