Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I reset this Health Bar when ever you die?

Asked by 3 years ago

Currently when you die in-game with this health bar then it will not reset and can't figure out how to reset it.

HealthHandler (Local Script)

local bar = script.Parent.Bar

local healthBar = script.Parent.Health


local plr = game.Players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()


local colorGreen = Color3.fromRGB(0, 255, 17)
local colorRed = Color3.fromRGB(255, 0, 0)


local tweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)


local goal = {}

goal.Size = nil

goal.ImageColor3 = nil


while wait() do

    if not char:FindFirstChild("Humanoid") then return end

    local health = char.Humanoid.Health

    local maxHealth = char.Humanoid.MaxHealth


    goal.Size = UDim2.new(bar.Size.X.Scale / maxHealth * health, 0, healthBar.Size.Y.Scale, 0)

    goal.ImageColor3 = colorRed:lerp(colorGreen, health / maxHealth)


    local tween = tweenService:Create(healthBar, tweenInfo, goal)


    tween:Play()


    tween.Completed:Wait()

    tween:Destroy()

end
0
put it in a regular screengui mishokirva 22 — 3y
0
did it work mishokirva 22 — 3y
0
what to you mean by a regular screengui SMHerobrine 2 — 3y
0
if you put it in a screengui it will reset after death mishokirva 22 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

just change the char variable whenever the character respawns

local bar = script.Parent.Bar
local healthBar = script.Parent.Health
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local colorGreen = Color3.fromRGB(0, 255, 17)
local colorRed = Color3.fromRGB(255, 0, 0)

local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
local goal = {}
goal.Size = nil
goal.ImageColor3 = nil

plr.CharacterAdded:Connect(function(newchar)
    char = newchar
end)

while wait() do

    if not char:FindFirstChild("Humanoid") then return end

    local health = char.Humanoid.Health
    local maxHealth = char.Humanoid.MaxHealth

    goal.Size = UDim2.new(bar.Size.X.Scale / maxHealth * health, 0, healthBar.Size.Y.Scale, 0)
    goal.ImageColor3 = colorRed:lerp(colorGreen, health / maxHealth)

    local tween = tweenService:Create(healthBar, tweenInfo, goal)
    tween:Play()
    tween.Completed:Wait()
    tween:Destroy()
end
0
thx SMHerobrine 2 — 3y
Ad

Answer this question