I'm trying to make a custom health bar for my enemy NPC. There is a Frame inside of a screenGUI representing the enemy health. The Humanoid for the enemy named "StalinCat" is called enemy. Here's my script.
local enemy = game.Workspace.StalinCat.Enemy
local frame = script.Parent
local function onHealthChanged()
local percent = enemy.Health/enemy.MaxHealth
frame.Size = UDim2.new(0,percent*554,0,7)
print("Health = ", enemy.Health)
print("MaxHealth = ", enemy.MaxHealth)
end
enemy.Changed:Connect(onHealthChanged)
onHealthChanged()
When I damage the enemy, the health bar does not change. Only when the enemy is dead does the health bar become zero. I tried printing out the enemy's health and max health while I was debugging, both of them output 44(The max health), even when the enemy was damaged.
What am I doing wrong? `

2 comments
Thank you for your response! I replaced my code with yours, however, I'm getting the same issue. The healthchanged event is not firing when I am clearly damaging the enemy, and the onHealthChanged function doesn't run.
That code works for me, maybe it's because the script can't find a Humanoid named "Enemy" in "StalinCat". Try using WaitForChild on the Enemy Humanoid.