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
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
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