Non-humanoid healthbar help

Hello guys, I’m starting programming in studio, and I came across the following problem (or maybe confusion?):

I want to make the hp bar go down in a more natural way, but I just can’t do it the right way, The “npc” is not a humanoid, I am trying to make an enemy out of a block and then I intend to style it… But first I need this bar to be working.
image

local frame = script.Parent
local maxhp = script.Parent.Parent.Parent.Parent.Parent.Stats.Maxhp
local hp = script.Parent.Parent.Parent.Parent.Parent.Stats.HP
local clicker = script.Parent.Parent.Parent.Parent.Parent.ClickDetector
local barpixel = 140

function hpbar()		
	frame.Size = UDim2.new(0, (barpixel - (140/maxhp.Value)), 0, 25)
	barpixel = barpixel - (barpixel/maxhp.Value)
	
	if hp.Value == 0 then
		frame.Size = UDim2.new(0, 0, 0, 25)
		frame.Visible = false
		barpixel = 140
		wait(0.2)
		frame.Parent.Visible = false
		wait(4.8)
	end
	
	if hp.Value == maxhp.Value then
	    frame.Visible = true
	    frame.Parent.Visible = true
		frame.Size = UDim2.new(0, 140, 0, 25)
	end
end

hp.Changed:Connect(hpbar)

I am still learning to program so it’s normal for some things to seem a bit confusing, but I thank you in advance.

I recommend you to look into these two useful things:

The first is GetPropertyChangedSignal which can be used to detect whenever the health changes. It can be connected to a function and inside said function, you would use tweenservice to tween the size of the healthbar from the current size of the healthbar to whatever it would be after it’s calculated using the health value.

2 Likes

Can you be little more clear with what you want please? When you say that you want a “natural way” for the bar to go down, I’m assuming that you want it to smoothly move to the new HP value instead of quickly changing its position.

If this is what you want to do, I suggest looking into Tween Service, as @teamboy321 said

1 Like

I want the bar to go down according to the HP value, which is not happening in the situation, as I cannot find such a solution.
The youtube tutorials only explain about humanoids…

Make a intValue in the stats folder and name it currentHealth, or something similar based on your preference. And as I previously mentioned, use currentHealth:GetPropertyChangedSignal (obviously you would need to make a reference it). After this connect it to a function, and inside use tweenservice with a relatively small time so it updates quick.

1 Like

Thanks a lot, it works nice now!

1 Like