I am trying to make a Custom Health Bar with a damage stacking effect. What I mean by that is making an effect that stacks damage the player takes, by making a white bar represent the accumulated damage which goes away after 2 seconds of not taking damage.
I accomplish this using TweenService.
I have tried looking on the Dev Forum for posts similar to mine and have struggled replicating the same effect. My main issue is both executing the last tween and not to have my previous tween override the last one.
I have tried using tick() and os.time() but didn’t manage to do it.
I have also tried adding delay to my tween but it just didn’t start nor did the other damageFrame tween start.
I will attach a video of what it looks like now.
This is the script:
-- Variables
local parentFrame = script.Parent
local screenGui = parentFrame.Parent
local damageFrame = parentFrame.TakingDamage
local healthFrame = parentFrame.Health
local healthText = parentFrame.HealthText
local players = game:GetService("Players")
local ts = game:GetService("TweenService")
local plr = players.LocalPlayer
local connection
local healthTweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
-- Script
-- MAIN FUNCTION
local function updateHealthBar(Humanoid, newHealth)
local currentHealth = newHealth or Humanoid.Health
local maximumHealth = Humanoid.MaxHealth
local percentage = currentHealth / maximumHealth -- Calculates percentage (in decimal number between 0 and 1)
healthText.Text = math.round(currentHealth).."/"..maximumHealth -- Sets text for health
local tween = ts:Create(healthFrame, healthTweenInfo, {Position = UDim2.new(-1 + percentage, 0, 0, 0)}) -- Creates health tween for the green bar
tween:Play()
tween = ts:Create(damageFrame, healthTweenInfo, {Position = UDim2.new(1, 0, 0, 0)}) -- Moves the TakingDamage frame bar to default position
tween:Play()
tween = ts:Create(damageFrame, healthTweenInfo, {Size = UDim2.new(-1 + percentage, 0, 1, 0)}) -- Creates damage tween for the white bar
tween:Play()
-- THIS IS THE MAIN TWEEN I'M REFERRING TO
task.wait(2)
tween = ts:Create(damageFrame, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Position = UDim2.new(0, 0, 0, 0)}) -- Supposed to move the damage bar out of the frame in order to be invisible
tween:Play()
end
local function healthChangedConnection(Character)
if connection == nil then
local Humanoid = Character:WaitForChild("Humanoid")
connection = Humanoid.HealthChanged:Connect(function(newHealth) -- Connect function to event
updateHealthBar(Humanoid, newHealth)
end)
end
end
plr.CharacterAdded:Connect(healthChangedConnection)
plr.CharacterRemoving:Connect(function() -- Removes connection to update healthbar once character gets removed
if connection ~= nil then
connection:Disconnect()
connection = nil
end
end)
if plr.Character then
healthChangedConnection(plr.Character) -- Automatically update healthbar when player spawns
end
If you have any idea on how to fix any issues or have a completely different approach, please reach out. Thanks in advance.