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

How do I make a health bar GUI go down when the player gets damaged?

I am making a game, and I need a health bar to lower when the player gets damaged. The code I've been trying is:

local Frame = script.Parent.Frame.Frame
local player = game.Players.LocalPlayer.Character

player:WaitForChild("Humanoid"):GetPropertyChangedSignal("Health"):Connect(function()
		Frame.Size.X.Offset = player.Humanoid.Health*3.45
end)

It doesn't work, if you have a solution, I would be glad if you shared it with me, Thanks in advance.

1 answer

✓ Accepted answer

Hello, Ghost40Z!

You can use Health/MaxHealth and set the result as the frame scale, remember to have the frame parented to another frame or gui object, so the scale max will be the parent's size

local Frame = script.Parent.Frame.Frame
local player = game.Players.LocalPlayer.Character

player:WaitForChild("Humanoid"):GetPropertyChangedSignal("Health"):Connect(function()
        Frame.Size = UDim2.new(player.Humanoid.Health/player.Humanoid.MaxHealth, 0, 1, 0) -- This division will aways give a number between 0 and 1, so we know the percent of players health
end)

5 comments

0

This doesn't seem to work.

0

Try now, I edited the script

0

Still nothing. Sorry for the late response, I was kind of busy.

0

I edited it again, now it will work, I tested

0

It works now, Thank you very much!

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