Health bar script help

I am trying to scale the size of a health bar with a player’s health.

Below is the code I am using to try this

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Character = script.Parent
local Humanoid = Character:WaitForChild"Humanoid"

local health = Humanoid.Health

while true do
	wait(1)
	Humanoid.HealthChanged:Connect(function()
		Player.PlayerGui.UserInterface.HealthBG.HealthBar.Size.X.Scale = health/100
	end)
	
	if Humanoid.Health < 85 then
		print("in danger!")
		Player.PlayerGui.UserInterface.DangerLabel.Visible = true
		wait(10)
		Humanoid.Health += 5
	else	
		Player.PlayerGui.UserInterface.DangerLabel.Visible = false
	end
end

And below is the error I get when damaging my character

 Stack Begin  -  Studio
 Script 'Workspace.woloexe.HealthHandler', Line 12  -  Studio - HealthHandler:12
 Stack End  -  Studio 

Another issue that I often run into is that the health change function will run multiple times even when the health value in the humanoid changes only once

Any help would be extremely appreciated

Well I see some problems with the code. Firstly, your connection a new event handler function to Humanoid.Health changed every 1 second! There is no reason to do this, you only need to connect it once. Second, you cannot change the scale property of a Udim2 directly as it’s immutable, you would have to create a new UDm2 value.

Additionally, your setting the “Health” variable once to Humanoid.Health. But your not updating that whenever the health changes, so when you calculate the new HealthBar.Size.X.Scale your actually just using the same value.


if Humanoid.Health < 85 then
		print("in danger!")
		Player.PlayerGui.UserInterface.DangerLabel.Visible = true
		wait(10)
		Humanoid.Health += 5
	else

Also waiting inside the while loop while adding health is a bad idea, because if the player’s health changes while your script is yielding, it will still run the Humanoid.Health += 5 after the yield is over. Also there is a chance of “dead code running” if this script is cloned every-time the character respawns (if having Gui.ResetOnSpawn enabled)

Last issue is Humanoid.Health is being updated on the client! Which is not what we want we want it to be updated on the server so it replicates. So what I would recommend is splitting the healing code into a server script while the UI logic into a local script inside the UI.

Here is the code implemented fixing these issues,

Here would be the healing logic implemented

local RunService = game:GetService("RunService")
local HealSpeed = 5

RunService.Heartbeat:Connect(function(Delta)
	for i,v in game.Players:GetPlayers() do
		if v.Character == nil then continue end
		if v.Character:FindFirstChild("Humanoid") == nil then continue end
		
		if v.Character.Humanoid.Health < 85 then
			v.Character.Humanoid.Health += Delta * HealSpeed
		end
	end
end)

Here would be the UI logic implemented

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

local UserInterface = Player.PlayerGui:WaitForChild("UserInterface")

Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local Health = Humanoid.Health	
	local HealthBar = UserInterface.HealthBG.HealthBar
	
	HealthBar.Size = UDim2.fromScale(Health/100, HealthBar.Size.Y.Scale)
	UserInterface.DangerLabel.Visible = if Humanoid.Health < 85 then true else false
end)
1 Like

the ui code works perfectly fine, but the heal script seems to not really do anything when i run. no errors, just does not heal the player. ive tried to place it in a server script in serverscriptservice and inside the character, but no luck.

it seems to only run the healing part of the code when the character dies

Hmm when I test the code it does run, I think your changing the health on the client by accident, or try increasing the HealSpeed if it’s changing the health to slowly. (Remember it only runs once the Humanoid.Health is under 85)

1 Like

nevermind, silly mistake. i was running the damage code through the client console instead of the server one lmao

thank you a lot for the help though

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