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

x.Size = UDim2.new(Hp.Value /100, 0, 1, 0)

if Hp.Value < 0 then

    Hp.Value = 0

elseif Hp.Value > 100 then

    Hp.Value = 0
end

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.

local player = game.Players.LocalPlayer
repeat wait() until player.Character --Waits until their character spawns in
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local bar = script.Parent

hum.HealthChanged:connect(function()
    bar:TweenSize(UDim2.new((hum.Health / hum.MaxHealth), 0, 0, 15), "Out", "Sine", 0.2, true)
end)

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

local width = bar.Size.X.Offset

--To the part where it sets the scale 

UDim2.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