Table sorting is not working

I’m trying to sort LandParts, which are parts that the capital will expand its land to, which I’m trying to do in the script as seen by comparing there positions and finding the closest ones to the capital.

The issue is, It doesn’t print out the part that had the position, and some of the table assets are duplicated.

How can I fix this script that’s supposed to sort different parts positions to the capital parts position in order, and then get the part closest to it?

 local capital = script.Parent

local conqueringRegion = Region3.new(Vector3.new(43, .5, -14), Vector3.new(20, 5, 10))
local conqueringRegionVisual = Instance.new("Part")

local LandPartTable = workspace:WaitForChild("LandPartTable"):GetChildren()

local landClosestTo = {}

while task.wait(1) do
	conqueringRegionVisual.Anchored = true
	conqueringRegionVisual.Parent = workspace
	conqueringRegionVisual.Transparency = 0.8
	conqueringRegionVisual.Size = Vector3.new(math.abs(conqueringRegion.Size.X), math.abs(conqueringRegion.Size.Y), math.abs(conqueringRegion.Size.Z))
	conqueringRegionVisual.CFrame = conqueringRegion.CFrame

	for _, landPart in pairs(LandPartTable) do
		table.insert(landClosestTo, (landPart.Position - capital.Position).Magnitude)
	end
	
	table.sort(landClosestTo)
	
	print(landClosestTo)

	print(landClosestTo[1])

	for i, v in pairs(landClosestTo) do
		table.remove(landClosestTo, v)
	end
end

I think you’re missing stuff to table.sort

--sort from closest to farthest
	table.sort(landClosestTo, function(a,b)
		return a < b
	end)

For some reason, two more items have been included in the table after the loop ran a few more times, and I’m still not sure about the root cause of this.

  ▼  {
                    [1] = 0.5,
                    [2] = 4.031128883361816,
                    [3] = 4.031128883361816,
                    [4] = 5.678908348083496,
                    [5] = 8.015609741210938,
                    [6] = 8.015609741210938,
                    [7] = 8.958236694335938,
                    [8] = 8.958236694335938,
                    [9] = 11.32475185394287,
                    [10] = 12.01041221618652,
                    [11] = 12.01041221618652,
                    [12] = 12.65898895263672,
                    [13] = 12.65898895263672,
                    [14] = 14.43087005615234,
                    [15] = 14.43087005615234,
                    [16] = 16.97792625427246
                 }  -  Server - Expand:25
  18:46:51.982  0.5  -  Server - Expand:27
  18:46:52.996   ▼  {
                    [1] = 0.5,
                    [2] = 4.031128883361816,
                    [3] = 4.031128883361816,
                    [4] = 5.678908348083496,
                    [5] = 8.015609741210938,
                    [6] = 8.015609741210938,
                    [7] = 8.958236694335938,
                    [8] = 8.958236694335938,
                    [9] = 8.958236694335938,
                    [10] = 11.32475185394287,
                    [11] = 12.01041221618652,
                    [12] = 12.01041221618652,
                    [13] = 12.65898895263672,
                    [14] = 12.65898895263672,
                    [15] = 14.43087005615234,
                    [16] = 14.43087005615234,
                    [17] = 16.97792625427246,
                    [18] = 16.97792625427246
                 }  -  Server - Expand:25
  18:46:52.996  0.5  -  Server - Expand:27
  18:46:54.015   ▼  {
                    [1] = 0.5,
                    [2] = 4.031128883361816,
                    [3] = 4.031128883361816,
                    [4] = 5.678908348083496,
                    [5] = 8.015609741210938,
                    [6] = 8.015609741210938,
                    [7] = 8.958236694335938,
                    [8] = 8.958236694335938,
                    [9] = 8.958236694335938,
                    [10] = 11.32475185394287,
                    [11] = 12.01041221618652,
                    [12] = 12.01041221618652,
                    [13] = 12.65898895263672,
                    [14] = 12.65898895263672,
                    [15] = 14.43087005615234,
                    [16] = 14.43087005615234,
                    [17] = 14.43087005615234,
                    [18] = 16.97792625427246
                 }  -  Server - Expand:25
  18:46:54.016  0.5  -  Server - Expand:27
  18:46:55.028   ▼  {
                    [1] = 0.5,
                    [2] = 4.031128883361816,
                    [3] = 4.031128883361816,
                    [4] = 5.678908348083496,
                    [5] = 8.015609741210938,
                    [6] = 8.015609741210938,
                    [7] = 8.958236694335938,
                    [8] = 8.958236694335938,
                    [9] = 8.958236694335938,
                    [10] = 11.32475185394287,
                    [11] = 12.01041221618652,
                    [12] = 12.01041221618652,
                    [13] = 12.65898895263672,
                    [14] = 12.65898895263672,
                    [15] = 14.43087005615234,
                    [16] = 14.43087005615234,
                    [17] = 14.43087005615234,
                    [18] = 16.97792625427246
                 }  -  Server - Expand:25
  18:46:55.029  0.5  -  Server - Expand:27
local capital = script.Parent

local conqueringRegion = Region3.new(Vector3.new(43, .5, -14), Vector3.new(20, 5, 10))
local conqueringRegionVisual = Instance.new("Part")

local LandPartTable = workspace:WaitForChild("LandPartTable"):GetChildren()

local landClosestTo = {}

while task.wait(1) do
	conqueringRegionVisual.Anchored = true
	conqueringRegionVisual.Parent = workspace
	conqueringRegionVisual.Transparency = 0.8
	conqueringRegionVisual.Size = Vector3.new(math.abs(conqueringRegion.Size.X), math.abs(conqueringRegion.Size.Y), math.abs(conqueringRegion.Size.Z))
	conqueringRegionVisual.CFrame = conqueringRegion.CFrame

	for _, landPart in pairs(LandPartTable) do
		table.insert(landClosestTo, (landPart.Position - capital.Position).Magnitude)
	end
	
	table.sort(landClosestTo, function(a,b)
		return a < b
	end)
	
	print(landClosestTo)

	print(landClosestTo[1])

	for i, v in pairs(landClosestTo) do
		for i = 1, #landClosestTo do
			table.remove(landClosestTo, i)
		end
	end
end

There are a few issues with the script that you provided. Firstly, you are inserting the distance between each land part and the capital into the landClosestTo table, but you’re not storing the actual land part object anywhere. This means that you cannot tell which land part corresponds to each distance in the table.

Secondly, when you’re iterating over the landClosestTo table to remove the elements, you are using the values themselves as the indices to remove. This is incorrect since the table.remove function expects an index, not a value.

Here’s an updated version of your script that should address these issues:

local capital = script.Parent

local conqueringRegion = Region3.new(Vector3.new(43, .5, -14), Vector3.new(20, 5, 10))
local conqueringRegionVisual = Instance.new("Part")

local LandPartTable = workspace:WaitForChild("LandPartTable"):GetChildren()

while true do
    conqueringRegionVisual.Anchored = true
    conqueringRegionVisual.Parent = workspace
    conqueringRegionVisual.Transparency = 0.8
    conqueringRegionVisual.Size = Vector3.new(math.abs(conqueringRegion.Size.X), math.abs(conqueringRegion.Size.Y), math.abs(conqueringRegion.Size.Z))
    conqueringRegionVisual.CFrame = conqueringRegion.CFrame

    -- Create a new table to store the distances and corresponding parts
    local distancesToCapital = {}

    -- Iterate over the LandPartTable and add each part's distance and object to the new table
    for _, landPart in pairs(LandPartTable) do
        distancesToCapital[landPart] = (landPart.Position - capital.Position).Magnitude
    end

    -- Sort the table by distance in ascending order
    table.sort(distancesToCapital, function(a, b) return a < b end)

    -- Get the first part in the sorted table (closest to capital)
    local closestPart = next(distancesToCapital)

    -- Print the closest part's position and distance to capital
    print(closestPart.Position, distancesToCapital[closestPart])

    -- Wait for some time before updating again
    wait(1)
end

In this updated version, we create a new table distancesToCapital that stores each land part object and its distance to the capital. We then sort this table by distance in ascending order, and retrieve the first item in the table (which should be the closest part to the capital). We then print the closest part’s position and its distance to the capital.

Note that I also removed the landClosestTo table and the loop that tries to remove its elements. We don’t need this since we are storing the distance and object in a single table. Additionally, I changed the infinite loop to use while true instead of while task.wait(1) since we don’t actually need to wait for a specific task to complete before updating the distances again. Finally, I removed the unnecessary parentheses around the landPart.Position - capital.Position expression.

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