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

Why is my health bar not tweening?

i'm making a health bar and after taking damage it moves but only the first time.

Here is my script

local human = game.Players.LocalPlayer.Character:WaitForChild('Humanoid')
local health = human.Health
local maxhealth = human.MaxHealth
local hp = health / maxhealth


human.Changed:Connect(function(child)
	if child == "Health" then
	script.Parent:TweenSize(UDim2.new(hp / 2.5,0,0,46),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.5)
	wait()
	end
end)

1 answer

✓ Accepted answer

You calculate hp once at the top of the script when you should be calculating it inside the changed event if you want the new value. It is more performant to use GetPropertyChangedSignal here as well. https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal

Try this instead:

-- Make sure the character exists before trying to access the humanoid
local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:wait()
end
local humanoid = character:WaitForChild("Humanoid")

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	-- Calculate hp here to get current values
	local health = humanoid.Health
	local maxHealth = humanoid.MaxHealth
	local hp = health / maxHealth

	script.Parent:TweenSize(UDim2.new(hp / 2.5, 0, 0, 46), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 0.5)
end)

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