Health Bar not working (UI)

Hey! I was making a health bar script for my player UI, but script is not working, the script is located inside the health bar UI frame. Here’s the script:

local player = game.Players.LocalPlayer
local char = player.Character
local gui = script.Parent
local frame = gui.HealthFrame
local bar = frame.Bar
local text = frame.HealthText

while wait() do

text.Text = math.floor(char:WaitForChild("Humanoid").Health).."/"..char:WaitForChild("Humanoid").MaxHealth

bar.Size = UDim2.new(0,(char:WaitForChild("Humanoid").Health / char:WaitForChild("Humanoid").MaxHealth * 200),1,0)
end

Here’s an explorer screenshot:
Captura de pantalla 2023-09-09 a las 23.00.48
HealthFrame is the health bar frame, “salud” is the script witch contains the script witch I’ve shared on this post, then “HealthText” Is where I want to display the health text number, witch I wanto to being displayed as this: 100/100

If someone could help, I would really apreciate!

3 Likes

i think this would help

Edit: i have realised, you are using a global script and not a localscript, can you try changing it to that and try?

Script “salud” needs to be a LocalScript

2 Likes

Instead of creating a while loop that continuously updates the UI, even when there are no changes applied to the player’s health, consider doing something like this:

-- Services
local Players = game:GetService("Players")

-- Constants
local Player = Players.LocalPlayer

-- Variables
local Character, Humanoid

-- Main
local function SetupHumanoid()
    if not Humanoid then
        return
    end

    Humanoid:GetPropertyChangedSignal("Health", function()
        -- update the UI
    end
end

local function OnCharacterAdded(NewCharacter)
    Character = NewCharacter
    Humanoid = Character and Character:WaitForChild("Humanoid", 5)

    if Humanoid then
        SetupHumanoid()
    else
        warn("Could not retrieve Humanoid") -- delete this if you want, simply for error catching
    end
end

if Player.Character then
    OnCharacterAdded(Player.Character)
end

Player.CharacterAdded:Connect(OnCharacterAdded)

This way, you only update the UI if the player’s health changes, which is more optimized and should do exactly what you’re looking for. Lmk if you need any further help or have any questions!

1 Like

Please, run this in command bar

local salud = game.StarterGui["Player UI"].Frame.HealthFrame.salud local newsalud = Instance.new("LocalScript") newsalud.Name = "salud" newsalud.Source = salud.Source newsalud.Parent = salud.Parent salud:Destroy()

I’ve tried on localscript and not worked anyways.

I’ve made local script as I said to @builderinbusiness but not working.

Maybe you can try this line:


bar.Size = UDim2.fromScale(char:WaitForChild("Humanoid").Health / char:WaitForChild("Humanoid").MaxHealth ,0)

I have been trying that script on localscript format and on script too, not working, did I need to change anything from there or that should work, because in that case, not working too.

1 Like

Should I add it as new code line? In that case, on witch of the two scripts from this post?

No, replace it with the bar.Size line.

local player = game.Players.LocalPlayer
local char = player.Character
local gui = script.Parent
local frame = gui.HealthFrame
local bar = frame.Bar
local text = frame.HealthText

while wait() do

text.Text = math.floor(char:WaitForChild("Humanoid").Health).."/"..char:WaitForChild("Humanoid").MaxHealth

bar.Size = UDim2.fromScale(char:WaitForChild("Humanoid").Health / char:WaitForChild("Humanoid").MaxHealth ,0)


end

This code will get the health from 0 to 1, 0 is 0 and 1 is the maximum health. And it will change the frame size accordingly to its parent

So, I tried that as you said and wrote on the script but on localscript or script too is not working.

i see the problem. on the explorer, the “salud” script is the one you’re mentioning right? look how the script is trying to get healthframe, the problem is, script.Parent IS healthframe and you didnt need to find healthframe in healthframe.
image

local player = game.Players.LocalPlayer
local char = player.Character
local frame = script.Parent
local bar = frame.Bar
local text = frame.HealthText

while wait() do

text.Text = math.floor(char:WaitForChild("Humanoid").Health).."/"..char:WaitForChild("Humanoid").MaxHealth

bar.Size = UDim2.fromScale(char:WaitForChild("Humanoid").Health / char:WaitForChild("Humanoid").MaxHealth ,0)


end

You have to replace the -- update the UI part in the SetupHumanoid function with your UI, so something like this:

-- Services
local Players = game:GetService("Players")

-- Constants
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local PlayerUI = PlayerGui:WaitForChild("Player UI")
local MainFrame = PlayerUI:WaitForChild("Frame")
local HealthFrame = MainFrame:WaitForChild("HealthFrame")

-- Variables
local Character, Humanoid

-- Main
local function SetupHumanoid()
    if not Humanoid then
        return
    end

    Humanoid:GetPropertyChangedSignal("Health", function()
        -- update the UI
        HealthFrame.Bar.HealthText.Text = `{tostring(math.floor(Humanoid.Health))}/{tostring(Humanoid.MaxHealth)}`
        HealthFrame.Bar.Size = UDim2.fromScale(math.clamp(Humanoid.Health / Humanoid.MaxHealth, 0, 1), 1)
    end
end

local function OnCharacterAdded(NewCharacter)
    Character = NewCharacter
    Humanoid = Character and Character:WaitForChild("Humanoid", 5)

    if Humanoid then
        SetupHumanoid()
    else
        warn("Could not retrieve Humanoid") -- delete this if you want, simply for error catching
    end
end

if Player.Character then
    OnCharacterAdded(Player.Character)
end

Player.CharacterAdded:Connect(OnCharacterAdded)

This way, you’re incorporating the UI changes whenever the player’s health changes in a much more optimized way, but still achieving your health bar.

Place this script into a LocalScript parented under StarterPlayerStarterPlayerScripts and it should work!

Don’t forget to mark this post as a solution if this helps :slightly_smiling_face:

Hello! And thanks for trying helping me out on this. I’ve tried your script and it’s still not working, LocalScrit is located inside the frame of the health bar, I’ll send some pictures & code.
Here’s the code (LocalScript):

-- Services
local Players = game:GetService("Players")

-- Constants
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local PlayerUI = PlayerGui:WaitForChild("Player UI")
local MainFrame = PlayerUI:WaitForChild("Frame")
local HealthFrame = MainFrame:WaitForChild("HealthFrame")

-- Variables
local Character, Humanoid

-- Main
local function SetupHumanoid()
	if not Humanoid then
		return
	end

	Humanoid:GetPropertyChangedSignal("Health", function()
		-- update the UI
		HealthFrame.Bar.HealthText.Text = `{tostring(math.floor(Humanoid.Health))}/{tostring(Humanoid.MaxHealth)}`
		HealthFrame.Bar.Size = UDim2.fromScale(math.clamp(Humanoid.Health / Humanoid.MaxHealth, 0, 1), 1)
	end
end

local function OnCharacterAdded(NewCharacter)
	Character = NewCharacter
	Humanoid = Character and Character:WaitForChild("Humanoid", 5)

	if Humanoid then
		SetupHumanoid()
	else
		warn("Could not retrieve Humanoid") -- delete this if you want, simply for error catching
	end
end

if Player.Character then
	OnCharacterAdded(Player.Character)
end

Player.CharacterAdded:Connect(OnCharacterAdded)

Here is the explorer:


So “HealthFrame” Is the frame where it’s the textlabel for display the player’s health, etc. Where LocalScript is where actually it’s located the script, and the HealthText is the label where should display the text.

Hello! Yeah this script worked, thank you so much for the help!

1 Like