Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a health bar?

Asked by 8 years ago

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

Log in to vote
1
Answered by 8 years ago

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

0
You were very helpful! Thank you for showing me your youtube video! BloodySoldier2002 10 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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
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 :) Uroxus 350 — 8y

Answer this question