CCCPaste Login

Health Bar System

local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:FindFirstChild("Humanoid")

local HealthBar = script.Parent.HealthBackground.HealthBar
local PercentageLabel = HealthBar.Parent.TextLabel

local function UpdateHealthBar()
	local XSize = math.clamp(Humanoid.Health / Humanoid.MaxHealth, 0, 1)
	HealthBar:TweenSize(UDim2.fromScale(XSize, 1), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5)
	PercentageLabel.Text = `{tostring(math.floor(XSize * 100))}%`
end

UpdateHealthBar()

Humanoid:GetPropertyChangedSignal("Health"):Connect(UpdateHealthBar)
Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(UpdateHealthBar)