Is this a good script for a game health bar

Hey guys, I want to make a health bar for a game and I found this script and I added on to it.

But I need your option if this is good or not.

Thanks and here’s the script:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild(“Humanoid”)

– Create a simple health bar
local healthBar = Instance.new(“Frame”)
healthBar.Name = “SimpleHealthBar”
healthBar.AnchorPoint = Vector2.new(0.5, 0.5)
healthBar.Position = UDim2.new(0.5, 0, 0.9, 0) – Bottom center of screen
healthBar.Size = UDim2.new(0.3, 0, 0.03, 0) – Width, Height
healthBar.BackgroundColor3 = Color3.new(0.2, 0.2, 0.2)
healthBar.BorderSizePixel = 1
healthBar.Parent = player.PlayerGui:WaitForChild(“PlayerGui”) or player:WaitForChild(“PlayerGui”)

local healthFill = Instance.new(“Frame”)
healthFill.Name = “HealthFill”
healthFill.Size = UDim2.new(1, 0, 1, 0) – Starts full
healthFill.BackgroundColor3 = Color3.new(1, 0, 0) – Red
healthFill.BorderSizePixel = 0
healthFill.Parent = healthBar

– Update health bar when health changes
humanoid.HealthChanged:Connect(function(health)
local healthPercent = health / humanoid.MaxHealth
healthFill.Size = UDim2.new(healthPercent, 0, 1, 0)

-- Change color based on health (optional)
if healthPercent > 0.5 then
    healthFill.BackgroundColor3 = Color3.new(0, 1, 0) -- Green when above 50%
else
    healthFill.BackgroundColor3 = Color3.new(1, 0, 0) -- Red when below 50%
end

end)

  • Is this a good health bar script
  • Yes
  • No
0 voters
  • Would this script work?
  • Yes
  • No
0 voters
2 Likes

its ok but i recomend you to not check color and instead do lerping to archive cooler smooth effect:

--Caching values
local Green:Color3 = Color3.new(0,1,0)
local Red:Color3 = Color3.new(1,0,0)
local Lerp = Red.Lerp
--
humanoid.HealthChanged:Connect(function(health):()
local healthPercent:number = health / humanoid.MaxHealth
healthFill.Size = UDim2.new(healthPercent, 0, 1, 0)

healthFill.BackgroundColor3 = Lerp(Red,Green,healthPercent)
end)

2 Likes

Code Review

1 Like

It wont show percents, as you were trying to achieve that I think. Bcs it’ll get a decimal, which you need to convert:

local healthPercent = health / humanoid.MaxHealth * 100 .. "%"
3 Likes