Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Ghost40Z 118
4 years ago
Edited by Leamir 4 years ago

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

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
0
This doesn't seem to work. Ghost40Z 118 — 4y
0
Try now, I edited the script Leamir 3138 — 4y
0
Still nothing. Sorry for the late response, I was kind of busy. Ghost40Z 118 — 4y
0
I edited it again, now it will work, I tested Leamir 3138 — 4y
0
It works now, Thank you very much! Ghost40Z 118 — 4y
Ad

Answer this question