Health Bar Frame Error

  1. **What do you want to achieve? This script throws a frame error. I would like the error not to ocurr.

  2. *What is the issue? The script throws this error

HealthStatus is not a valid member of Frame “Players.Darkwulf7777.PlayerGui.HealthBarGui.HealthBar”

Anyone know why this error happens?


game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)


local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local healthbar = script.Parent:WaitForChild("HealthBar")


game:GetService("RunService").RenderStepped:Connect(function()
	local humanoid = char:FindFirstChild("Humanoid")
	healthbar.GreenFrame.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth,0,1,0)
	healthbar.HealthStatus.Text = math.floor(humanoid.Health).."/"..humanoid.MaxHealth
end)

I think the issue is that it’s still trying to load everything inside your PlayerGui, even though you called WaitForChild() on your HealthBar it’s still trying to load the rest of the UI Objects

You should also used a HealthChanged Event instead of a RenderStepped, that’d would way easier

Try this:


game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)


local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local Humanoid = char:WaitForChild("Humanoid")
local healthbar = script.Parent:WaitForChild("HealthBar")
local HealthStatus = healthbar:WaitForChild("HealthStatus")
local GreenFrame = healthbar:WaitForChild("GreenFrame")

Humanoid.HealthChanged:Connect(function(NewHealth)
	GreenFrame.Size = UDim2.new(NewHealth/Humanoid.MaxHealth,0,1,0)
	HealthStatus.Text = math.floor(NewHealth).."/"..Humanoid.MaxHealth
end)
1 Like

That got rid of the error, but the gui stopped working. Stopped showing health being subtracted while taking damage.

Can you add print statements to check where the issue specifically lies at?

I found a new tutorial and this one seems to work.

game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local frame = script.Parent

local plr = game:GetService("Players").LocalPlayer

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

local hum = char:WaitForChild("Humanoid")

hum.HealthChanged:Connect(function()

local percentage = hum.Health / hum.MaxHealth

if(percentage >=0) then

frame.Health:TweenSize(UDim2.fromScale(percentage, frame.Health.Size.Y.Scale))

end

frame.HealthLabel.Text = math.floor(percentage * 100) .. "%"

end)

StarterGui gets replicated to the Player and becomes PlayerGui. You need to change your code to cater for this. Run the game up in Studio and look at the Player and it’s sub-items, where you will see the full structure.