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

I have been having problems with a custom health bar, any idea on why?

I have been having trouble with making a custom health bar. What will happen is that it works when you are losing health and die, but it will immediately stop working after you respawn into the game.

local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local TweenService = game:GetService("TweenService")

hum.Changed:connect(function(health)
	if health == "Health" or health == "MaxHealth" then
	    local bar = script.Parent
	    local info = TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
	    local goal = {["Size"] = UDim2.new((hum.Health/hum.MaxHealth), 0, 1, 0)}
	
	    TweenService:Create(bar, info, goal):Play()
	    script.Parent.Parent.TextLabel.Text = ("Health: " .. math.floor(hum.Health).."/".. math.floor(hum.MaxHealth))
	end
end)

Any help is appreciated.

4 comments

0

Where is your script located? If you can, please add a picture of its hierarchy.

0

Maybe the GUI is like "ResetOnSpawn" to false?

0

I don't think its necessarily a problem with the actual code of the script I think the problem lies within the function, what I did to test this was I put print(hum.Health) inside of the code and it only printed 0 a couple of times and then stopped altogether. Im not completely sure why it didn't update to the point where it went back up to maxhealth.

0

There is already are tweening methods: TweenSize, TweenPosition, TweenSizeAndPosition, you don't really need to use the garbage tweenservice. kek

1 answer

add this at the top and it should work

local char = player.Character
if not char or not char.Parent then
	char = player.CharacterAdded:wait()
end

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