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