Need help figuring out the formula for a health bar

I’m making a 3D health bar and I need help trying to figure out the formula for the offset position of this part. This is because, whenever I tween the size, both negative and positive sides on that axis (normalId’s) go inward when the tween is lower, and outward when the tween is higher.

I want to make it so that it looks like only one side is going up by tweening it the same way while also changing its position at the same time. But I need help finding a formula for this, and that’s what I can’t find.

I’ve got the tweening size part down, but how can I find the formula for the part that I’m tweening?

The Current Dimensions of the Green Bar is {10, 0.5, 1.5},

The Current Position of the Green Bar is {-44, 0.75, 33.5}

Script:

local player = game.Players.LocalPlayer
local viewport = script.Parent
local character = player.Character or player.CharacterAdded:Wait()

local GreenBar = viewport:WaitForChild("Health_Green")

local cam = Instance.new("Camera")
viewport.CurrentCamera = cam
cam.Parent = viewport

cam.CFrame = CFrame.new(Vector3.new(-24.672, 1.154, 38.232)) * CFrame.Angles(math.rad(2.702), math.rad(-16.649), math.rad(-0))

character:WaitForChild("Humanoid").HealthChanged:Connect(function()
	local sizeX = (character:WaitForChild("Humanoid").Health / character:WaitForChild("Humanoid").MaxHealth) * 10
	local tween = game:GetService("TweenService"):Create(GreenBar, TweenInfo.new(.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
		Size = Vector3.new(sizeX, GreenBar.Size.Y, GreenBar.Size.Z)
	})
	
	local posX = 1 -- will be changed after I find the formula.

	local posTween = game:GetService("TweenService"):Create(GreenBar, TweenInfo.new(.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
		Position = Vector3.new(posX, GreenBar.Position.Y, GreenBar.Position.Z)
	})

	tween:Play()
	posTween:Play()
end)

So you have a health bar that goes down when something takes damage? So currently when tweening, the health bar goes right to the middle?

1 Like

Correct, and so Im trying to make another tween for offset. But, the issue js, I dont have a formula for tweening the Xpos.

posX = -4.4 + sizeX/2
This formula takes the current size of the health bar (sizeX), divides it by 2 to get the half-size, and then subtracts 4.4 to adjust for the base position. This will give you the new X position for the health bar. Once you have the new X position, you can create a tween that moves only the X position of the health bar using the formula:
Position = Vector3.new(posX, GreenBar.Position.Y, GreenBar.Position.Z)
This will create a tween that only moves the X position of the health bar, while keeping the Y and Z positions constant.

Here is a script that I have for my NPC’s

local MaxHealthGUISizeX = .76

local Num = MaxHealthGUISizeX / script.Parent.MaxHealth.Value

local TotalNum = Num * script.Parent.Health.Value

script.Parent.GUI.Green.Size = UDim2.new(TotalNum,0,--[[Ysize]],0)