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.