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

Can somebody help with this health bar?

Asked by 10 years ago

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()

01x.Size = UDim2.new(Hp.Value /100, 0, 1, 0)
02 
03if Hp.Value < 0 then
04 
05    Hp.Value = 0
06 
07elseif Hp.Value > 100 then
08 
09    Hp.Value = 0
10end

end

Hp.Changed:connect(Change)

1 answer

Log in to vote
3
Answered by 10 years ago

Well you could use a local script so that you can use LocalPlayer rather than doing script.Parent.Parent.Parent etc.

1local player = game.Players.LocalPlayer
2repeat wait() until player.Character --Waits until their character spawns in
3local char = player.Character
4local hum = char:WaitForChild("Humanoid")
5local bar = script.Parent
6 
7hum.HealthChanged:connect(function()
8    bar:TweenSize(UDim2.new((hum.Health / hum.MaxHealth), 0, 0, 15), "Out", "Sine", 0.2, true)
9end)

if you are using offset for the x size you could do

1local width = bar.Size.X.Offset
2 
3--To the part where it sets the scale
4 
5UDim2.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

0
Thanks this really helped ! kamontae123 0 — 10y
Ad

Answer this question