I'm trying to create a tracker that leads to a player, but I'm having trouble with offsetting the bar between the local player, and target. The green bar should becomes invisible when the player is not facing the direction of the target.

Here's how it currently functions and the entire script.

https://vimeo.com/398960643

local smoothness = 30/3

wait(1)

local follow = game.Workspace.King1
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

local dev = script.Parent

local lastY = 0

function restrictAngle(angle)
	if angle < -math.pi then
		return angle + math.pi*2
	elseif angle > math.pi then
		return angle - math.pi*2
	else
		return angle
	end
end

while true do
	local delta = wait(1/30)

	local look = camera.CoordinateFrame.lookVector
	local look = Vector3.new(look.x, 0, look.z).unit
	local lookY = math.atan2(look.z, look.x)

	local difY = restrictAngle(lookY - lastY)
	lookY = restrictAngle(lastY + difY*delta*smoothness)
	lastY = lookY
		
		
		local XPosOffset = (follow.HumanoidRootPart.Position.X)
		local ZPosOffset = (follow.HumanoidRootPart.Position.Z)
		
		playerLocation = math.pi * XPosOffset/ZPosOffset
		
		bar = dev.Middle
		rot = restrictAngle(lookY - playerLocation)
		if math.sin(rot) > 0 then
			local cosRot = math.cos(rot)
			local cosRot2 = cosRot*cosRot

			bar.Visible = true
			bar.Position = UDim2.new(0.5 + cosRot*0.6, bar.Position.X.Offset, 0, 3)

			local total = (follow:WaitForChild("HumanoidRootPart").Position + player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
			local magnitude = (follow:WaitForChild("HumanoidRootPart").Position - player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
			
			bar.Size = UDim2.new(0, 10, 0.1, 0)
			
		else
			bar.Visible = true
		end
	end