How to check if a players health changes?

Hey!

Is it possible to check if a players health changes, like using GetPropertyChangedSignal? I am currently building a Health Bar GUI in my game, and I am not wanting to use a ‘while do true’ term in my script, because I want to prevent a loop in my GUI.

Here is my script:

wait(3) -- use to wait for game to load the players character

--// Disable Health Bar
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

-- //varibles
local player = game:GetService("Players").LocalPlayer
local char = player.Character

local HealthFrame = script.Parent

game:GetService("RunService").RenderStepped:Connect(function()
	local humanoid = char:FindFirstChild("Humanoid")
	HealthFrame.GreenFrame.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth, 0,1,0)
	HealthFrame.Health.Text = math.floor(humanoid.Health).."/"..humanoid.MaxHealth
end)

I am wanting to check the if the players health changes becuase when you start the game you start with infinite health, so no one can kill you. Please let me know how I can fix this issue in my game. Thanks!

Sincerely,

papahetfan

wait(3) -- use to wait for game to load the players character

--// Disable Health Bar
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

-- //varibles
local player = game:GetService("Players").LocalPlayer
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")

local HealthFrame = script.Parent

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	HealthFrame.GreenFrame.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth, 0,1,0)
	HealthFrame.Health.Text = math.floor(humanoid.Health).."/"..humanoid.MaxHealth
end)
1 Like

Humanoid.HealthChanged will fire whenever the humanoid’s health changes.

Try this script and see if it works. It uses the GetPropertyChangedSignal on your character’s Humanoid. edit: you have to put char before humanoid for the signal to be humanoid’s health! Cheers

wait(3) -- use to wait for game to load the players character

--// Disable Health Bar
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

-- //variables
local player = game:GetService("Players").LocalPlayer
local char = player.Character

local HealthFrame = script.Parent

char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local humanoid = char:FindFirstChild("Humanoid")
	HealthFrame.GreenFrame.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth, 0,1,0)
	HealthFrame.Health.Text = math.floor(humanoid.Health).."/"..humanoid.MaxHealth
end)