Health bar damage effect not working

“What is the desired result?”
When taking damage, the health that got taken should turn orange and slide into the rest of the health bar after a delay (see video).


“What is the issue?”
Everything seemingly works until the tween, where it suddenly chooses to not move (see output).
“What have you tried?”
I’ve searched through forums and made lots of tweaks, but to no avail.

The script:

-- setup
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- tweening info
local info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,1)
local target = UDim2.new(0, humanoid.Health/humanoid.MaxHealth*500, 0, 50)
-- the tween that doesn't work for some reason
local tween = TweenService:Create(script.Parent, info, {Size = target})
-- trigger tween when health changes
humanoid.HealthChanged:Connect(function()
    print("Second number should be", humanoid.Health/humanoid.MaxHealth*500)
    print("Size:", script.Parent.Size)
    tween:Play()
end)

The bar has been made transparent and set on top for easy viewing. The text not appearing will be for another post.

The result:

This is my first attempt at coding in Roblox, so additional help would be appreciated.

1 Like

Move the local tween = into the HealthChanged function. It likely IS tweening, but it’s just tweening to the initial size, rather than the intended size

I tried

humanoid.HealthChanged:Connect(function()
	local tween = TweenService:Create(script.Parent, info, {Size = target})
	print("Second number should be", humanoid.Health/humanoid.MaxHealth*500)
	print("Size:", script.Parent.Size)
	tween:Play()
end)

and the same thing without the tween:Play()
but it still doesn’t work.

I think the issue might be to do with the {Size} part of the tween. When using TweenService on a GUI Element, in order to change the size, you should use UDim2 (which can be used for changing the size of screen elements), taking the X and Y sizes to change. So, when doing so to the bar, you could try something like:

local tween = TweenService:Create(script.Parent, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Size = UDim2.fromScale(xSize, ySize)})

tween:Play()

The scaling uses the offset, not the scale. When trying fromOffset, however, the bug persists.

-- setup
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local Bar = [your reference] this is the main bar that tweens
local BackupBar = [your reference] This is the bar that tweens a couple seconds after the Bar

humanoid.HealthChanged:Connect(function()
      TweenService:Create(Bar, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {Size = UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0)}):Play()
task.wait(0.7)
   TweenService:Create(Bar, TweenInfo.new(0.4, Enum.EasingStyle.Sine), {Size = BackupBar.Size}):Play()
end)

Let me know if this works and the text label not appearing is probably an issue with ur ui layout

Forgot to say that you need to move the target variable thing into the HealthChanged thing as well:

-- setup
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- tweening info
local info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,1)

humanoid.HealthChanged:Connect(function()
    local target = UDim2.new(0, humanoid.Health/humanoid.MaxHealth*500, 0, 50)
    local tween = TweenService:Create(script.Parent, info, {Size = target})
    print("Second number should be", humanoid.Health/humanoid.MaxHealth*500)
    print("Size:", script.Parent.Size)
    tween:Play()
end)

This new script gives a very… strange effect.

im gonna try modifying your code a little bit beyond just rearranging. It may make things worse depending on how you have the UI set up

-- setup
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- tweening info
local info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,1)

humanoid.HealthChanged:Connect(function()
    local target = UDim2.new(humanoid.Health/humanoid.MaxHealth, 0, 0, 50)
    local tween = TweenService:Create(script.Parent, info, {Size = target})
    print("Firstnumber should be", humanoid.Health/humanoid.MaxHealth)
    print("Size:", script.Parent.Size)
    tween:Play()
end)

This also causes that weirdness from before.
Not sure if this context helps, but the other health bar uses this script:

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

humanoid.HealthChanged:Connect(function()
	script.Parent.Size = UDim2.new(0, humanoid.Health/humanoid.MaxHealth*500, 0, 50)
end)