Health Bar Help

Want: Basic health bar that goes down or up based of player’s health

Problem: Bar is not displaying health.

Background: I have looked in other forums and didn’t find the fix. The script is a server script

Placement:


Code:

local Humanoid = script.Parent.Parent.Parent.Parent.Parent.Parent.Humanoid

Humanoid.HealthChanged:Connect(function()
	script.Parent.Size = UDim2.new(1, 0, Humanoid.Health / Humanoid.MaxHealth , 0)
end)
2 Likes

Use GetPropertyChangedSignal to detect health change, it’s more reliable

why is it a serverscript, change it to a localscript and instead of using many parents to refer to the humanoid just do it like so

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")

Humanoid.HealthChanged:Connect(function()
	script.Parent.Size = UDim2.new(1, 0, Humanoid.Health / Humanoid.MaxHealth , 0)
end)

I want all player to see health, would it still work if it was local script?

1 Like

I tried GetPropertyChangedSignal and it still has the same problem. I also added a print and nothing printed

Code:

local humanoid = script.Parent.Parent.Parent.Parent.Parent.Parent.Humanoid

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local healthRatio = humanoid.Health / humanoid.MaxHealth
	script.Parent.Size = UDim2.new(healthRatio, 0, 1, 0)
    print("Health changed to:", humanoid.Health)
end)
2 Likes

Make the AnchorPoint 0,0 and put the bar in the same position as before. Bar = UDim2.new(0,0,0,0) AnchorPoint = 0,0

1 Like

I have made it like that. All the frames AnchorPoint 0.5,0.5 beside the Bar

1 Like

Are you damaging the humanoid on the client?

Also :WaitForChild(“Humanoid”) would be better just in case

Convert it to a local script THEN use a :FireServer to call the health change to the server to make it visible to everybody else. We can get the script from @ayoub50 and change it a bit

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local RemoteEvent = remoteevent
Humanoid.HealthChanged:Connect(function()
	RemoteEvent:FireServer(Humanoid.Health)
end)

And do whatever in the server script,

local RemoteEvent = remoteevent
RemoteEvent.OnServerEvent:Connect(function(plr, health)
plr.Character.SurfaceGui.HealthBar.Frame.Bar.Size = UDim2.new(1, 0, Humanoid.Health / Humanoid.MaxHealth , 0)
end)

Also, what are you changing your health from…? the server or client?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.