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

Health Bar doesn't work, color doesn't change and long decimal numbers??[Solved]

Asked by 6 years ago
Edited 6 years ago

Hello everyone who reads this question, I want to make a health bar for my game, however, it doesn't change the color of the bar when the health is at a certain point

The hierarchy is game>StarterGui/PlayerGui>HealthUI>HealthBar>GetPlayerHealth[The script is a local script] Plus I also noticed another thing, it returns a float(A decimal number but not waaaaaaay to exact), I have tried math.floor()(math.floor rounds down a number) and math.ceil()(math.ceil rounds up a number) but it doesn't work(I have deleted that piece of code)

-- Variables --
local starterUI = game:GetService("StarterGui")
local player = game.Players.LocalPlayer
local character = player.Character
local hum = character:FindFirstChild("Humanoid")
local bar = script.Parent
-- Loops -- 
while wait() do
    bar.Text = hum.Health .. " / " .. hum.MaxHealth
end
while wait() and (hum.Health <= 70 and hum.Health >= 50) do
    bar.BackgroundColor3 = Color3.fromRGB(234, 255, 41) -- This is the same thing as Color3.new(234/255,255/255,41/255) for the people who didn't know that
end
while wait() and (hum.Health <= 50 and hum.Health >= 30)do
    bar.BackgroundColor3 = Color3.fromRGB(207, 178, 70)
end
while wait() and (hum.Health <= 30 and hum.Health >= 0)do
    bar.BackgroundColor3 = Color3.fromRGB(207, 9, 9)
end

It would be much appreciated if you answer this question

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I got it to work!!!

-- Variables --
local starterUI = game:GetService("StarterGui")
local player = game.Players.LocalPlayer
local character = player.Character
local hum = character:FindFirstChild("Humanoid")
local bar = script.Parent
-- Loops -- 
while wait() do
    bar.Text = hum.Health .. " / " .. hum.MaxHealth
    wait(0.5)
    hum.Health =  math.floor(hum.Health + 1)
    if hum.Health <= 70 and hum.Health >= 50 then
        bar.BackgroundColor3 = Color3.fromRGB(234, 255, 41)
    end
    if hum.Health <= 50 and hum.Health >= 30 then
        bar.BackgroundColor3 = Color3.fromRGB(207, 178, 70)
    end
    if hum.Health <= 30 and hum.Health >= 0 then
        bar.BackgroundColor3 = Color3.fromRGB(207, 9, 9)
    end
end
0
I highly suggest taking a look at my answer. Otherwise, congratulations on getting your script to work the way you want it. User#18718 0 — 6y
0
Time for UDim2 saSlol2436 716 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Okay one of the problems I see immediately is the use of waits and while loops to check for conditions. This is a no-no in my book. Here's how I would alter your code using the HealthChanged event of the Humanoid class (http://wiki.roblox.com/index.php?title=API:Class/Humanoid/HealthChanged).

-- Variables --
local starterUI = game:GetService("StarterGui")
local player = game.Players.LocalPlayer
local character = player.Character
local hum = character:FindFirstChild("Humanoid")
local bar = script.Parent
-- Events --
hum.HealthChanged:Connect(function(newHealth)
    bar.Text = hum.Health .. " / " .. hum.MaxHealth
    if health <= 70 and health >= 50 then
        bar.BackgroundColor3 = Color3.fromRGB(234, 255, 41)
    elseif health <= 50 and health >= 30 then
        -- ... etc you get the point.
    end
end)

Also Health is a float. A double has double the precision of a float (in a simplified way of explaining it ... more decimal places)

EDIT: Using Connect. I was around before Connect. oldman

0
I got it to work anyway, however there is one thing I must say, use Connect instead of connect because connect is depricated saSlol2436 716 — 6y
0
Fixed. User#18718 0 — 6y
0
They both work!! saSlol2436 716 — 6y
0
How would one convert a float into an interger?? I have tested it out with both methods however it doesn't work. saSlol2436 716 — 6y
View all comments (7 more)
0
I understand where you got the health changed I feel like the would be a more important way to do because I will have animations whenever the player is damaged saSlol2436 716 — 6y
0
math.floor and math.ciel should work.They round up and down. math.floor(hum.Health + 0.5) will get you that sweetspot. User#18718 0 — 6y
0
Thanks saSlol2436 716 — 6y
0
However where would I put it, in the beggining of the event?? saSlol2436 716 — 6y
0
I got it to work, but I have one thing, whenever the health changes then the real health pops up, I have fixed that and I added a regenerating part, so yeah, thanks! saSlol2436 716 — 6y
0
You should set it equal to a variable. Like ``local roundedHealth = math.floor(health + 0.5)`` then use roundedHealth in the GUI bit. User#18718 0 — 6y
0
I fixed it but thanks for the suggestion saSlol2436 716 — 6y

Answer this question