-- ================================================================================
-- PUBLIC FUNCTIONS
-- ================================================================================

function ArrowGuide.new(targets)
	local arrow = Instance.new("Part")
	arrow.Name = "Arrow"
	arrow.Size = Vector3.new(2, 1, 5)
	arrow.Color = Color3.new(252/255, 1/255, 7/255) -- Red
	arrow.CanCollide = false
	
	local mesh = Instance.new("SpecialMesh")
	mesh.MeshId = "http://www.roblox.com/asset/?id=14656345" -- Arrow mesh
	mesh.Scale = Vector3.new(0.2, 0.4, 0.2)
	mesh.Parent = arrow
	
	arrow.Position = character.PrimaryPart.Position + Vector3.new(0, character.PrimaryPart.Size.Y + OFFSET, 0)
	
	local arrowWeld = Instance.new("Weld")
	arrowWeld.Part0 = arrow
	arrowWeld.Part1 = character.PrimaryPart
	
	arrow.Parent = character
	
	RunService:BindToRenderStep("UpdateDirection", 1, function()
		if targets == 0 then return end
	
		local checkpoint = targets[1]:FindFirstChild("Checkpoint")
		local closestDistance = (arrow.Position - checkpoint.Position).magnitude
		local closestTarget = checkpoint
	
		-- Find nearest target
		for _, target in ipairs(targets) do
			local hit = target:FindFirstChild("Hit")
			if (not hit or hit.Value == false) then
				checkpoint = target:FindFirstChild("Checkpoint")
				local distance = (arrow.Position - checkpoint.Position).magnitude
				if distance < closestDistance then
					closestTarget = checkpoint
				end
			end
		end
	
		-- Point arrow at nearest target
		local newPos = character.PrimaryPart.Position + Vector3.new(0, character.PrimaryPart.Size.Y + OFFSET, 0)
		arrow.CFrame = CFrame.new(newPos, closestTarget.Position)
	end)
	
	return arrow
end -- ArrowGuide.new()