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

How to make a HP bar appear WHEN you join?

I was trying to make a health bar that shows on top of you, it's working fine until I realized that it doesn't appear when you JOIN the game. It appears right after you die. How do I fix that?

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local bill = game.ReplicatedStorage.BillboardGui:Clone()
        bill.Frame.Visible = true
        bill.Frame1.Visible = true
        bill.TextLabel.Visible = true
        
        bill.Parent = char.Head
        
        bill.TextLabel.Text = plr.Name

        char:WaitForChild("Humanoid").Changed:Connect(function()
            bill.Frame.Size = UDim2.new(char:WaitForChild("Humanoid").MaxHealth,0,0.3,0)
            bill.Frame1.Size = UDim2.new(char:WaitForChild("Humanoid").Health / char:WaitForChild("Humanoid").MaxHealth,0,0.3,0)
        end)
    end)
end)

Could anyone help me with this?

1 answer

Its because its going to fast to get the characteradded. Use this below

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait() --Issue fixed.
        local bill = game.ReplicatedStorage.BillboardGui:Clone()
        bill.Frame.Visible = true
        bill.Frame1.Visible = true
        bill.TextLabel.Visible = true

        bill.Parent = char.Head

        bill.TextLabel.Text = plr.Name

        char:WaitForChild("Humanoid").Changed:Connect(function()
            bill.Frame.Size = UDim2.new(char:WaitForChild("Humanoid").MaxHealth,0,0.3,0)
            bill.Frame1.Size = UDim2.new(char:WaitForChild("Humanoid").Health / char:WaitForChild("Humanoid").MaxHealth,0,0.3,0)
        end)
end)

1 comment

0

Ok, this did fix the joining, but however, whenever I die, it doesn't appear anymore.

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