Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a reason on why this health.Changed script won't work?

wait(game.Lighting.TimeToLoad.Value)

-- Health Bar
local HealthB = script.Parent.bk.HBar

repeat until game.Players.LocalPlayer.CharacterAdded:Wait()

game.Players.LocalPlayer.Character.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local redzone = game.Players.LocalPlayer.Character.Humanoid.MaxHealth / 2
	if game.Players.LocalPlayer.Character.Humanoid.Health <= redzone then
		HealthB.ImageColor3 = Color3.new(255, 0, 0)
	end
	if game.Players.LocalPlayer.Character.Humanoid.Health > redzone then
		local brickcolor = BrickColor.new("Lime green")
		local color = brickcolor.Color
		HealthB.ImageColor3 = color
	end
	HealthB.Size = UDim2.new(game.Players.LocalPlayer.Character.Humanoid.Health / game.Players.LocalPlayer.Character.Humanoid.MaxHealth, 0, 0, 20)
end)

1 answer

You cannot add a Changed event to a property like that. It just won't work.

The way to do it is as follows:

game.Players.LocalPlayer.Character.Humanoid:GetPropertyChangedSignal("Health")
:Connect(function()

More info

repeat until game.Players.LocalPlayer.CharacterAdded:Wait()

repeat until are completely unnecessary and are also permanently running, preventing your event from connecting.

7 comments

0

I was actually using a ImageLabel as the health bar, i updated the question. It still doesn't work even if i used the :GetPropertyChangedSigna;

0

Just noticed something before that. Updated Answer

0

Gui Object's changed event still fires from their position updating.

0

It works, but when the player's health is lowered it gets smaller and moves it position above the frame

0

Is this a new method? I've never seen it before. Looks neat!

0

Fairly new

0

The repeat until wouldn't run forever due to the fact that the CharacterAdded:Wait() will in fact return the character. It is useless though.

This discussion is preserved as it was. New answers, comments, and votes are closed.