"GetPropertyChangedSignal" doesn't find Health in humanoid

I am trying to make a health bar with a percentage number in the middle. The problem I am facing is with the GetPropertyChangedSignal which I have never used before. I know how it should work but doesn’t know why It doesn’t find Health from a humanoid of a local player.

The Local Script that I wrote

local player = game:GetService("Players").LocalPlayer
local playerHumanoid = player.Character or player.CharacterAdded:Wait().Humanoid

local uiScreen = game.StarterGui:WaitForChild("Screen")
local statFrame =  uiScreen.StatBar
local HealthBar = statFrame.HealthBar
local HealthPercentage = HealthBar.Percentage.Text

-- Health handler
local PrevPercentage = 100

local function OnHealthChanged()
	local healthFilled = HealthBar.Filled.Size
	local playerHealth = playerHumanoid.Health

	healthFilled:TweenSize(UDim2.new(playerHealth/100,0,1,0), nil, nil, 2)
	if PrevPercentage>playerHealth then
		for i = 1,(PrevPercentage - playerHealth),1 do
			HealthPercentage=(PrevPercentage-1)
		end
		PrevPercentage = playerHealth
	elseif PrevPercentage<playerHealth then
			for i = 1,(playerHealth - PrevPercentage),1 do
				HealthPercentage=(PrevPercentage+1)
			end
			PrevPercentage = playerHealth
	end
end

playerHumanoid:GetPropertyChangedSignal("Health"):Connect(OnHealthChanged())

The Output I get,

Health is not a valid property name.

1 Like

The reason is because if the character already exists, it will set it as the character, and it doesn’t it will set it as the humanoid, change it to this

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local playerHumanoid = char:WaitForChild("Humanoid")

On the first 3 lines

Also your event will not work, the last line has to be

playerHumanoid:GetPropertyChangedSignal("Health"):Connect(OnHealthChanged)

Inorder for it to work.

Also, I think you should use the HealthChanged event for this as it does what you’re trying to do but is a bit simpler to write code for, whenever the Health changes, call the event, exactly what you’re trying to do but it gives you the health as well so you don’t have to make a variable to get the health

1 Like

I didn’t knew about that HealthChanged function till now. Thanks in advanced!

1 Like

Glad to have taught you something new! If you have any more questions or issues don’t be afraid to make another psot!

1 Like