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.
-
What do you want to achieve? A working HP bar.
-
What is the issue? The title. It doesn’t work, and apparently isn’t finding the GUI.
-
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)