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

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

Asked by 6 years ago
Edited 6 years ago

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.

0
Where is your script located? If you can, please add a picture of its hierarchy. Le_Teapots 913 — 6y
0
Maybe the GUI is like "ResetOnSpawn" to false? Nep_Ryker 131 — 6y
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. MyTradeJustWentViral 5 — 6y
0
There is already are tweening methods: TweenSize, TweenPosition, TweenSizeAndPosition, you don't really need to use the garbage tweenservice. kek Fifkee 2017 — 6y

1 answer

Log in to vote
0
Answered by
Elixcore 1337 Moderation Voter
6 years ago
Edited 6 years ago

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
Ad

Answer this question