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

Can somebody help with this health bar?

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

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

1 comment

0

Thanks this really helped !

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