Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a health bar?

Hi,

I wanted to make a game that contains a health bar, stamina bar, hunger bar, and a thirst bar. I know how to use the math to keep track of the health, stamina, etc.. But I don't know how to make the health bar visually. Please help me with this. Thank you for reading and helping me with my problem.

Sincerely, BloodySoldier2002

2 answers

Simple division

The primary purpose of a health bar is to display a percentage indirectly with a GUI. All we have to do to get the percentage of something, is divide the difference by the initial value. In this case, the player's current health divided by their max health.

Explanation

I'd explain this more in depth, but I've actually created a YouTube video on this if you're interested. I go over the basis of creating and coding a custom health bar, along with providing the source code. You can find the video here: https://www.youtube.com/watch?v=opZtUhwDWE8

1 comment

0

You were very helpful! Thank you for showing me your youtube video!

Making the structure of the health bar can be self-explanatory.

However, this is the code:

local player = game.Players.LocalPlayer
local fill = script.Parent.Filler
local text = script.Parent.TextLabel
while wait() do
	local character = player.Character
	if character ~= nil then
		local human = character:FindFirstChild("Humanoid")
		if human ~= nil then
			fill:TweenSize(UDim2.new((human.Health/human.MaxHealth),0,1,0))
			text.Text = "HP: "..math.floor(human.Health).." / "..human.MaxHealth.." ["..math.floor((human.Health/human.MaxHealth)*100).."%]"
		end
	else
		text.Text = "NIL"
		fill:TweenSize(UDim2.new(0,0,1,0))
	end
end

1 comment

0

I wouldn't use the while loop as you're doing, for efficiency purposes. It would probably be best to use the .Changed function, however from the looks of this it would answer his question :)

This discussion is preserved as it was. New answers, comments, and votes are closed.