Array comparer inaccurate

still slightly confused…
the end result should be the array ‘itemstobe’?
do i need to loop through the other array to destroy each index within itemstobe?

For the code I submitted yes.

I think I’ve identified to original problems as well, it seems to have been due to the removal of items from the array while it was being looped through, and that the loop was rechecking old values, these can be fixed by adding an array to store the objects to remove afterwards, and by checking if the index of the secondary loop is greater than that of the primary loop:

function isValid()
	
	-- setup the arrays
	itemsToBe = {}
	local itemsToCompare = {}
	
	-- fill the compare array
	for _, v in pairs(cs:GetTagged("comparingBoundaries")) do
		table.insert(itemsToCompare, v)
	end
	
	-- loop through the compare array
	for i, u in pairs(itemsToCompare) do

		local xU = u:FindFirstChild("xCoord")
		local yU = u:FindFirstChild("yCoord")
		
		-- loop through the compare array to find a second item
		for j, v in pairs(itemsToCompare) do

			-- check if the item exist and if the secondary index is greater
			if not (v and u and j > i) then continue end 

			local xV = v:FindFirstChild("xCoord")
			local yV = v:FindFirstChild("yCoord")
			
			-- check if they are located at the same coordinates
			if xV.Value ~= xU.Value or yV.Value ~= yU.Value then continue end
			
			-- add the first item to the array to be removed if its name is main
			-- otherwise add the second item
			table.insert(itemsToBe, (v.Name == "main") and v or u)

		end

	end

end

this version should have the same itemToBe array as the original,

1 Like

Did some slight modifying to your code, but I took the general idea of filling up the final array rather than removing indices and it works like a charm!

Thanks so much for your help over the past day!! :pray::heart:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.