Code Issues - Tool Cloning in Specific Areas

Hi there! I’d like to ensure that the first cloned tool is placed in the first available delivery box and the second in the second available delivery box using the findNearestDeliveryAreas() function.

How can I achieve this? Also, how can I prevent two tools from being cloned in the same delivery box?
I need a hand with this… :pray:

server-side script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local deliverOrderFunction = ReplicatedStorage.remoteFunctions:WaitForChild("deliverOrderFunction")

local deliveryBoxesFolder = game.Workspace.Game_Elements.Deliverables.CheffDeliver_Area.deliverAreasBoxes

-- Function to find the nearest delivery areas to the player that do not have a tool cloned already
local function findNearestDeliveryAreas(player)
	local nearestAreas = {}
	local shortestDistances = {math.huge, math.huge}

	for _, area in pairs(deliveryBoxesFolder:GetChildren()) do
		if area:IsA("Part") then -- Check if the child is a Part
			local distance = (area.Position - player.Character.HumanoidRootPart.Position).magnitude
			if distance < shortestDistances[1] then
				-- Update the first nearest area
				shortestDistances[2] = shortestDistances[1]
				nearestAreas[2] = nearestAreas[1]

				shortestDistances[1] = distance
				nearestAreas[1] = area
			elseif distance < shortestDistances[2] then
				-- Update the second nearest area
				shortestDistances[2] = distance
				nearestAreas[2] = area
			end
		end
	end

	-- Check if there's already a tool cloned in each nearest area
	for i, area in ipairs(nearestAreas) do
		local tool = area:FindFirstChildOfClass("Tool")
		if tool then
			-- Remove the area from the list if there's already a tool cloned there
			table.remove(nearestAreas, i)
		end
	end

	return nearestAreas
end

deliverOrderFunction.OnServerInvoke = function(player, orderData, index)
	-- Print order details
	print("Item 1: " .. orderData.item1)
	print("Item 2: " .. orderData.item2)
	print("Total Price: $" .. orderData.totalprice)

	-- Check if the player has any tools in their backpack
	local backpack = player.Backpack
	if not backpack or #backpack:GetChildren() == 0 then
		-- Player has no tools in their backpack, return false
		return false, "Player has no tools to deliver the order"
	end

	-- Get the player's tools
	local tools = {}
	for _, tool in pairs(backpack:GetChildren()) do
		if tool:IsA("Tool") then
			tools[tool.Name] = tool
		end
	end

	-- Print player's tools
	print("Player's tools:")
	for toolName, _ in pairs(tools) do
		print(toolName)
	end

	-- Check if the player's tools match the items in the order
	if tools[orderData.item1] then
		-- Player has item1
		local nearestArea = findNearestDeliveryArea(player)
		if nearestArea then
			-- Clone item1 to the nearest delivery area
			local toolClone1 = tools[orderData.item1]:Clone()
			toolClone1.Parent = game.Workspace
			toolClone1:SetPrimaryPartCFrame(nearestArea.CFrame)
			toolClone1.PrimaryPart = toolClone1:FindFirstChild("PrimaryPart") or toolClone1.PrimaryPart

			-- Remove item1 from the player's Backpack
			tools[orderData.item1]:Destroy()
		else
			warn("Could not find a delivery area near the player.")
		end
	end

	if orderData.item2 ~= "" and tools[orderData.item2] then
		-- Player has item2 and it's not empty
		local nearestArea = findNearestDeliveryArea(player)
		if nearestArea then
			-- Clone item2 to the nearest delivery area
			local toolClone2 = tools[orderData.item2]:Clone()
			toolClone2.Parent = game.Workspace
			toolClone2:SetPrimaryPartCFrame(nearestArea.CFrame)
			toolClone2.PrimaryPart = toolClone2:FindFirstChild("PrimaryPart") or toolClone2.PrimaryPart

			-- Remove item2 from the player's Backpack
			tools[orderData.item2]:Destroy()
		else
			warn("Could not find a delivery area near the player.")
		end
	end

	return true, "Order successfully delivered"
end

console output: