So I'm trying to make this health bar that show the health of a player and the bar lowers however a player lose health but it isn't working.
I have a screengui into Playergui named Health, an ImageLabel named BackRound inside Health , another ImageLabel named Bar inside BackRound, and this script inside Bar
Hp = script.Parent.Parent.Parent.Parent.Parent.Character.Humanoid.Health
x = script.Parent
print("Running")
function Change()
01 | x.Size = UDim 2. new(Hp.Value / 100 , 0 , 1 , 0 ) |
02 |
03 | if Hp.Value < 0 then |
04 |
05 | Hp.Value = 0 |
06 |
07 | elseif Hp.Value > 100 then |
08 |
09 | Hp.Value = 0 |
10 | end |
end
Hp.Changed:connect(Change)
Well you could use a local script so that you can use LocalPlayer rather than doing script.Parent.Parent.Parent etc.
1 | local player = game.Players.LocalPlayer |
2 | repeat wait() until player.Character --Waits until their character spawns in |
3 | local char = player.Character |
4 | local hum = char:WaitForChild( "Humanoid" ) |
5 | local bar = script.Parent |
6 |
7 | hum.HealthChanged:connect( function () |
8 | bar:TweenSize(UDim 2. new((hum.Health / hum.MaxHealth), 0 , 0 , 15 ), "Out" , "Sine" , 0.2 , true ) |
9 | end ) |
if you are using offset for the x size you could do
1 | local width = bar.Size.X.Offset |
2 |
3 | --To the part where it sets the scale |
4 |
5 | UDim 2. new( 0 , (hum.Health / hum.MaxHealth) * width, 0 , 15 ) --The rest |
Also if 15 isn't your y offset size then change it, it was just an example
You don't have to tween but I think it looks nice