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

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

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

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

2 comments

0

I highly suggest taking a look at my answer. Otherwise, congratulations on getting your script to work the way you want it.

0

Time for UDim2

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

11 comments

0

I got it to work anyway, however there is one thing I must say, use Connect instead of connect because connect is depricated

0

Fixed.

0

They both work!!

0

How would one convert a float into an interger?? I have tested it out with both methods however it doesn't work.

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

0

math.floor and math.ciel should work.They round up and down. math.floor(hum.Health + 0.5) will get you that sweetspot.

0

Thanks

0

However where would I put it, in the beggining of the event??

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!

0

You should set it equal to a variable. Like local roundedHealth = math.floor(health + 0.5) then use roundedHealth in the GUI bit.

0

I fixed it but thanks for the suggestion

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