Tried to create a hp bar but its not working correctly

i wanted to create a hp bar and came up with this script

local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid",30)
local frame = script.Parent
local previous = 100
local size=190

local function update()
	if hum.Health<previous then
		local currenthp = 100-hum.Health
		local per1= currenthp/100
		local todec= size*per1
		frame.Size=UDim2.fromOffset(frame.Size.X.Offset-todec,26)
		previous=hum.Health
	elseif hum.Health>previous then
		local per = hum.Health-previous
		local per1 = per/100
		local toin = size*per1
		frame.Size=UDim2.fromOffset(frame.Size.X.Offset+toin,26)
		previous=hum.Health
	end
end


hum:GetPropertyChangedSignal("Health"):Connect(update)



the script is a child of a frame , when i change health it works fine but once i change health repeatedly the size of the frame gradually decreases and is different from the original value i want to know why it happens and how it can be prevented

You over complicated this script, it can be reduced to simply:

local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid",30)
local frame = script.Parent

local function update()
    frame:TweenSize(humanoid.Health/humanoid.MaxHealth, 0, 1, 0)
end


hum:GetPropertyChangedSignal("Health"):Connect(update)

Not this will only work if Frame is parented to a frame that’s size doesn’t change and is the size of the full health bar.

2 Likes

thnx 4 the reply, guess i gotta learn tween service then ;-; but still i would like to know why the original values are not maintained when hp is changed repeatedly

1 Like

I’m pretty sure it was your calculations that were off.

ya dividing and multiplying repeatedly did something i would like 2 know how to prevent it