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:
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
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!
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.
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
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.ParentIS healthframe and you didnt need to find healthframe in healthframe.
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 StarterPlayer → StarterPlayerScripts and it should work!
Don’t forget to mark this post as a solution if this helps
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)
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.