HP Bar not working

Hi there, dev’s. I recently made an HP Bar, but it isn’t working. Not much to say about this honestly, but I’ve tried everything I know, and still nothing works. Please help.

  1. What do you want to achieve? A working HP bar.

  2. What is the issue? The title. It doesn’t work, and apparently isn’t finding the GUI.

  3. What solutions have you tried so far? I tried looking on the dev hub, but it doesn’t really help much to be 100% honest.

Here’s the code. (This code is in a LocalScript, and the script itself is parented into StarterGui)

local Players = game.Players or game:GetService("Players")
local Teams = game.Teams
local Client = Players.LocalPlayer

local function GetGui(Player, Health, MaxHealth)
	local Gui = Player:WaitForChild("PlayerGui")
	if Gui ~= nil then
		print("it not nil guys")
		local HPBar = Gui:WaitForChild("HP Gui")
		if HPBar ~= nil then
			HPBar["HP Number"].Text = tostring(math.floor(Health).."/"..MaxHealth)
			HPBar["HP Bar"].Size = UDim2.new(Health/MaxHealth * 0.152, 0, 0.074, 0)
			print("Completed Change.")
		end
	end
end

local function GetHP(Character)
	local Humanoid = Character:WaitForChild("Humanoid")
	if Character ~= nil and Humanoid ~= nil and Players:GetPlayerFromCharacter(Character) then
		local Player = Players:GetPlayerFromCharacter(Character)
		if Player ~= nil and Player.Team == Teams.Chara then
			local CharaPlayer = Player
			local MaxHP = Humanoid.MaxHealth
			local CurrentHP = Humanoid.Health
			local Signal = Humanoid:GetPropertyChangedSignal("Health")
			Signal:Connect(function(HP)
				HP = CurrentHP
				GetGui(CharaPlayer, HP, MaxHP)
				print("Changed.")
			end)
		end
	end
end

game.Workspace.ChildAdded:Connect(function(Child)
	if Child.ClassName == "Model" and Players:GetPlayerFromCharacter(Child) then
		GetHP(Child)
	end
end)
1 Like

Hello,

I have a working HP bar, can you desribe the issue? Please show a picture of the error or something. Does the HP bar show up at all? Is it just shows up but when you are getting hit it doesn’t change the HP bar (this could be an issue from the humanoid that hit you isn’t scripted right).

Thanks,
Bloxer

No errors in the output. Nor does it change to the humanoid’s hp. It just remains as ?/? which is what I set the text as until the humanoid is found.

Hello,

After comparing your code with mine the only real difference is you use “tostring” I do not and my numbers show up as numbers. I would look at some documentation on tostring as I am unsure if you really need to make it a string. Hope that helps, also I can’t see your studio so if you could tell us if it prints anything at all, if the script is printing “it not nil guys” then your issue is after that line. I can’t see that so provide that information next time you post to better assist you.

Thanks,
Bloxer

Hi, I tried it and it didn’t work still. All it prints is this, which is from another script.
image

I’m guessing you’re using a server script but i’d rather just use a local script since it’s much easier and not complicated. You can use this on what i made today for this.

healthgui.rbxm (7.1 KB)

You can use it without credit. Also just place the health gui in StarterGui.

It. Is. A LocalScript. Didn’t you see my note from my first comment asking for help? It’s a LocalScript, but it isn’t working.

Oh, I didn’t see that. Sorry, my mistake.

No point in checking if the GUI is nil, a WaitForChild call will yield the thread until the requested object exists.

Okay, I’ll remove that then. Maybe that’s what’s stopping it.