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

How to make 3d lifebar that follow the player?

I am trying to make the math of a part following the player And the life of the player i want the Part green to diminish when the life of a player is diminshing but i also wanted to make it follow where te camera is looking at this is what i tried so far:

game.workspace:WaitForChild("Player")
while true do
	wait(0.01)
local AngleX, AngleY, AngleZ = workspace.CurrentCamera.CFrame:toEulerAnglesXYZ()
local PosX = game.workspace.Player.Head.CFrame.x
local PosZ = game.workspace.Player.Head.CFrame.z
local PosY = game.workspace.Player.Head.CFrame.y
local Rotation = CFrame.Angles(AngleX, AngleY, AngleZ)
local SizeX = game.workspace.PlayerLife.LifeBar.Size.X
game.workspace.PlayerLife.LifeBar.CFrame = Rotation + Vector3.new(PosX, PosY + 2, PosZ)
game.workspace.PlayerLife.LIfeRed.CFrame = Rotation + Vector3.new(PosX, PosY + 2, PosZ)
end

i tried things after but doesnt worked the green one wasnt following the red one or it wasnt working at all And its a bit laggy i don't know how to make it move smoothly

1 answer

✓ Accepted answer

I'm assuming this is in a Server Script, because you would want other players to see the health bar.

here is what I would do.

--[[ Unlimitus ]]--

function PlayerAdded(player)
	if player:IsA("Player") then
		local currentBar = nil
		local function CreateHealthBar()
			wait()
			if currentBar == nil then
				local bar = Instance.new("Part")
				bar.Name = "HealthBar"
				bar.Parent = player.Character
				bar.CanCollide = false
				bar.Anchored = true
				bar.Size = Vector3.new(2.5, 0.5, 0.5)
				bar.CFrame = player.Character.Head.CFrame * CFrame.new(0, 2, 0)
				bar.BrickColor = BrickColor.new("Bright red")
				
				local green = bar:Clone()
				green.Name = "Green"
				Instance.new("BlockMesh", green)
				green.Mesh.Scale = Vector3.new(1.05, 1.05, 1.05)
				green.Parent = bar
				green.Size = Vector3.new(2.5, 0.5, 0.5)
				green.BrickColor = BrickColor.new("Bright green")
				green.CFrame = bar.CFrame
				
				currentBar = bar
			end
		end
		
		local function Heartbeat()
			if currentBar ~= nil then
				currentBar.CFrame = player.Character.Head.CFrame * CFrame.new(0, 2, 0)
				
				if player.Character.Humanoid.Health > 0 then
					currentBar.Green.Size = Vector3.new(player.Character.Humanoid.Health / player.Character.Humanoid.MaxHealth * 2.5,0.5,0.5)
				else
					currentBar.Green.Transparency = 1
				end
				
				currentBar.Green.CFrame = currentBar.CFrame
			end
		end
		
		local function PlayerDied ()
			wait()
			currentBar = nil
		end
		
		CreateHealthBar()
		player.CharacterAdded:connect(CreateHealthBar)
		player.Character.Humanoid.Died:connect(PlayerDied)
		game:GetService("RunService").Heartbeat:connect(Heartbeat)
	end
end

game:GetService("Players").ChildAdded:connect(PlayerAdded) -- I know there is a PlayerAdded function, but I like this better.

1 comment

0

Thx for the awnser its what i wanted but when the green portion rezize it resize in the middle what i need is i need it to resize on the left side

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