How should I go about making this?

So I’m making a 3D Viewport frame that displays the player’s health, and I need help trying to figure out how I can find out the formula for the length of the X axis is, because for the longer the X axis will be, the more health the player has, vice versa.

I’m not sure though how I should go about calculating what the length should be on the X axis that correlates to the players health. (The Max size of X should be 7.)

Other Post Related

XSize = (CurrentHealth / MaxHealth) * 7

It doesn’t seem to be working for me, and I also don’t seem to be getting any errors.

Place: Brickbattle Arena Test Place - Roblox

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local viewport = script.Parent

local GreenBar = viewport:WaitForChild("Health_Green")

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

-- POS = -24.784, 0.965, 37.515

cam.CFrame = CFrame.new(Vector3.new(-24.784, 0.965, 37.515)) * CFrame.Angles(math.rad(3.307), math.rad(-23.772), math.rad(0))

Instance.new("Part"):Resize(Enum.NormalId.Left, -1)

Instance.new("Humanoid").HealthChanged:Connect(function(health)
	local size = (health / humanoid.MaxHealth) * 7
	GreenBar.Size = size
	
	print(size)
	print(GreenBar.Size)
end)
local player = game.Players.LocalPlayer
local humanoid = character:WaitForChild("Humanoid")

local viewport = script.Parent

local GreenBar = viewport:WaitForChild("Health_Green")

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

-- POS = -24.784, 0.965, 37.515

cam.CFrame = CFrame.new(Vector3.new(-24.784, 0.965, 37.515)) * CFrame.Angles(math.rad(3.307), math.rad(-23.772), math.rad(0))

local Part = Instance.new("Part"):Resize(Enum.NormalId.Left, -1)

local function CharacterAdded(char)
    character.Humanoid.HealthChanged:Connect(function()
        local sizeX = (health / humanoid.MaxHealth) * 7
	    GreenBar.Size = Vector3.new(sizeX, GreenBar.Size.Y, GreenBar.Size.Z)
    end
end
player.CharacterAdded:Connect(CharacterAdded)
if player.Character then
    CharacterAdded(player.Character)
end

How is this code with errors solve my issue in my post above? I’m a bit confused.

I wrote it at night and I don’t have error-correction things on this forum.

local player = game.Players.LocalPlayer
local viewport = script.Parent

local GreenBar = viewport:WaitForChild("Health_Green")

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

-- POS = -24.784, 0.965, 37.515

cam.CFrame = CFrame.new(Vector3.new(-24.784, 0.965, 37.515)) * CFrame.Angles(math.rad(3.307), math.rad(-23.772), math.rad(0))

local Part = Instance.new("Part"):Resize(Enum.NormalId.Left, -1)

local function CharacterAdded(char)
    char.Humanoid.HealthChanged:Connect(function()
        local sizeX = (char.Humanoid.Health / char.Humanoid.MaxHealth) * 7
	    GreenBar.Size = Vector3.new(sizeX, GreenBar.Size.Y, GreenBar.Size.Z)
    end
end
1 Like

I have a few different questions about the code you posted.

Why did you create a part, but not add it to the game?

Why did you create another Humanoid instead of using the one from the player?

Those were both my bad, I was trying to figure out how I can resize a specific axis, and forgot to take that out. And I made a new humanoid instance when I was trying to look at the methods, but forgot to switch it back to the variable of humanoid.

I tweaked the code a bit, and it works smoothly, but how can I use the :Resize method for a tween?

Because without using the :Resize method on the green part, it won’t be smooth at all when you take damage.
image

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

-- POS = -24.784, 0.965, 37.515

cam.CFrame = CFrame.new(Vector3.new(-24.784, 0.965, 37.515)) * CFrame.Angles(math.rad(3.307), math.rad(-23.772), math.rad(0))

local Part = Instance.new("Part"):Resize(Enum.NormalId.Left, -1)

character:WaitForChild("Humanoid").HealthChanged:Connect(function()
	local sizeX = (character:WaitForChild("Humanoid").Health / character:WaitForChild("Humanoid").MaxHealth) * 7
	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)})
	
	tween:Play()
end)