Tweening an ImageLabel

I am trying to tween the health bar but it’s tweening incorrectly and re-sizing it wrong once the character has been damaged and when they begin to heal it’s not correct.

Here is the script;

local Player = game:GetService("Players").LocalPlayer
local TweenService = game:GetService("TweenService")

local MainGUI = script.Parent.Parent.Parent
local MainFrame = MainGUI.HealthAndEnergy
local HealthLine = MainFrame.Health

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Time = 1

local Information = TweenInfo.new(Time, Enum.EasingStyle.Quad)

local function OnHealthChanged()
    local NewHealth = math.floor(Humanoid.Health)

    local Goal = {}
    Goal.Size = UDim2.fromScale(NewHealth / 300, 0.6)

    local Tween = TweenService:Create(HealthLine, Information, Goal)
	Tween:Play()
end

Humanoid:GetPropertyChangedSignal("Health"):Connect(OnHealthChanged)

And a video of it;

Goal.Size = UDim2.new(NewHealth / 300, 0, HealthLine.Size.Y.Scale, HealthLine.Size.Y.Offset)
1 Like

I will try that now, thank you.

This fixed the size issue but it didn’t fix the issue completely.

When I take damage the bar goes back right but it doesn’t go back to it’s normal size. The normal size of the ImageLabel is {0, 342},{0, 6} and when the character is damaged and begins to regenerate it is supposed to go back to that size.

local MaxHealth = Humanoid.MaxHealth

Goal.Size = UDim2.new(NewHealth / MaxHealth, 0, HealthLine.Size.Y.Scale, HealthLine.Size.Y.Offset)
1 Like

This worked but it still isn’t the normal size.

image

1 Like

Clone the bars and put them inside themselves. That’s the easiest solution.

2 Likes

When I clone it, I set the parent as the Main Bar and then I just tween the Duplicate Bar right?

1 Like

I am assuming by the heart reaction that I am supposed to do that, just heart this message if so.

1 Like

Should the script look like this?

local Player = game:GetService("Players").LocalPlayer
local TweenService = game:GetService("TweenService")

local MainGUI = script.Parent.Parent.Parent
local MainFrame = MainGUI.HealthAndEnergy
local HealthLine = MainFrame.Health
local DuplicateHealthLine = HealthLine.Bar

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Time = 1

local Information = TweenInfo.new(Time, Enum.EasingStyle.Linear)

local function OnHealthChanged()
    local NewHealth = math.floor(Humanoid.Health)
	local MaxHealth = Humanoid.MaxHealth

    local Goal = {}
	Goal.Size = UDim2.new(NewHealth / MaxHealth, 0, HealthLine.Size.Y.Scale, HealthLine.Size.Y.Offset)

    local Tween = TweenService:Create(DuplicateHealthLine, Information, Goal)
	Tween:Play()
end

Humanoid:GetPropertyChangedSignal("Health"):Connect(OnHealthChanged)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.