Problem with healthbar overhead

I basically got everything down but some reason the script wont work?

wait(0.33)
local gui = script.Parent.Parent.Parent
local char = gui.Parent
local hum = char.Humanoid
local maxHealth = hum.MaxHealth
local Health = hum.Health

function updateDisplay()
	script.Parent:TweenSize(UDim2.new(Health / maxHealth, 0, 0.05, 0))
end
hum.HealthChanged:Connect(updateDisplay)

Lua (and ofc. luau) variables are not pointers, whenever you create a variable you create a copy of what you’re putting into the variable.

Doing hum.Health won’t create a shortcut to hum.Health, it’ll copy what value is stored within hum.Health which is likely 100 assuming this runs whenever a character is added.

Luckily, .HealthChanged passes the current health of the humanoid it belongs to has so instead of doing local Health = hum.Health you can do;

local function updateDisplay(Health)
	script.Parent:TweenSize(UDim2.new(Health / maxHealth, 0, 0.05, 0))
end

Be aware, characters gain health over time and .HealthChanged fires even when someone’s health goes up so you should do something like this:

local HealthWas = hum.Health
local function updateDisplay(Health)
	if HealthWas > Health then
		script.Parent:TweenSize(UDim2.new(Health / maxHealth, 0, 0.05, 0))
	end
	
	HealthWas = Health
end
1 Like

My bad i thought you were creating a damage effect for a second ignore the second code snippet that would prevent your healthbar from gaining size when a character is healed :joy:

was about say, the bar is kinda slow and not really following the healthbar that roblox has

Ok, never mind i fixed it but thanks alot

1 Like