How would i make a Health Bar GUI with Tween Service?

Sorry if this is already made and i just made an extra post.
How would i create a tween for a health bar GUI?
I havent made a script cuz im pretty sure i would have to revamp the whole thing except variables to make tween.

You’d need a percentage for it.

local ts = game:GetService("TweenService")
local healthBar = --path to health bar here

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local ySize = --the y size of the bar, which will be kept the same during the tween

local function updateBar()
    local percent = math.floor((humanoid.Health / humanoid.MaxHealth) * 100)
    local info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
    local barTween = ts:Create(healthBar, info, {Size = UDim2.fromScale(percent, ySize)})
    barTween:Play()
end

--call the update function whenever needed
updateBar()

This is just a general idea of how it might be done.
I hope this helps!

do you know what may be the error?

local ts = game:GetService("TweenService")
local healthBar = script.Parent

	local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local ySize = 30

	local function updateBar()
		local percent = math.floor((humanoid.Health / humanoid.MaxHealth) * 100)
		local info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
		local barTween = ts:Create(healthBar, info, {Size = UDim2.fromScale(percent, ySize)})
		barTween:Play()
	end

--call the update function whenever needed
updateBar()--Update The Health Bar

humanoid:GetPropertyChangedSignal("Health"):Connect(updateBar()) --Update The Health Bar When The Health Change
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(updateBar()) --Update The Health Bar Wheb The MaxHealth Change

error message: Attempt to connect failed: Passed value is not a function
the local script is in a frame, which is in a screenGUI.

Don’t put brackets in the Connect. It should just be:
:Connect(updateBar)

The percentage you’ve mentioned

This should be within the decimal format (0 and 1)
Since Udim2 accept percentage as a decimal form.

{ Size = UDim2.fromScale( ( humanoid.Health / humanoid.MaxHealth ) , ySize) }

Will do.

1 Like