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

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

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

4 comments

0

put it in a regular screengui

0

did it work

0

what to you mean by a regular screengui

0

if you put it in a screengui it will reset after death

1 answer

✓ Accepted answer

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

1 comment

0

thx

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