Why don't my tables put the instances in the right order

I’m making a pathfinding script for a Npc, and I’m making it so you can change the path whenever you want with dots. but when I use my script and print the table it, doesn’t do them in order, look.

                [1] = Start,
                [2] = void,
                [3] = WayPoint1,
                [4] = WayPoint2,
                [5] = WayPoint4,
                [6] = WayPoint5,
                [7] = WayPoint6,
                [8] = WayPoint7,
                [9] = WayPoint3,
                [10] = End

My script puts them in order of values I put in the parts, and I have all the values in order but it doesn’t do what I want it to. As you can see waypoint3 is in the 9th spot, and I also have a void instance for no reason. Heres a part of the code

local Waypoints = workspace.WayPointPath


local WayPointTable = {
}

if Waypoints then
	for i, Input in pairs(Waypoints:GetChildren()) do
		local Value = Input:FindFirstChild("Value")
		if Value.Value >= 0 then

			table.insert(WayPointTable, Value.Value, Input)
			
		end
		wait()
		print(#WayPointTable)
	end
	print(#WayPointTable)
	table.insert(WayPointTable, (#WayPointTable+1), Waypoints:FindFirstChild("End"))
end

How do I fix this, does anyone know?

I recommend you double check the value of the part by printing it out. It is also possible because of that “Start” part that it is ignoring the values.

1 Like

I have checked the values more than twice, Also the start one has a value of 1 and the Waypoint1 has a value of 2 and so on

So then where is that “void” coming from?

1 Like

I don’t know, I was hoping someone else would know
It just appears there
and replaces the spot of waypoint3

I am not sure either :man_shrugging:
Maybe try changing the loop to loop through the number of instances.

It is looping though the number of instances and is checking it’s value, once the value is check it inserts a instance in to the waypointTable and sets it’s position in the tables to the value

Where are you getting the waypoints from?

local Waypoints = workspace.WayPointPath

Should be:

local waypoints = path:GetWaypoints()

Each element in the returned table would then be a PathWaypoint | Roblox Creator Documentation and they should be in order.

1 Like

Waypoints is a line of cubes in my workspace, I’m not using pathfinder because I’m making a tower defense like system, Look

All the red blue and green cubes are waypoints
The red one is the end block, the green is start, and the blue ones are the waypoints
image

2 Likes

Try this:

local Waypoints = workspace.WayPointPath
local WayPointTable = {}

if Waypoints then
	local point = Waypoints:GetChildren()
	for i = 1, #point do
		local value = point[i]:FindFirstChild("Value")
		if(value and value.Value > 0)then
			WayPointTable[value.Value] = point[i]
		end
	end
	table.insert(WayPointTable, (#WayPointTable+1), Waypoints:FindFirstChild("End"))
end

Edit: I just realized as well that it should just be > 0 instead of >=0
And I’m sorry that also wouldn’t give you the right result. I changed it so use the value.Value number as the index otherwise it still wouldn’t have been in order.

1 Like

That seemed to work, But why? also what’s the difference between > and >= because the start is always above 0, and the end is = -1 so that it doesn’t get caught in the loop

Also!, what if my parts are not named and aren’t in order, wouldn’t it just mess up

Well the first index number in a table is always 1, there is no index 0.
I didn’t know what the End.Value.Value was so I assumed it was zero.
The snippet i gave ignores the names except for the name of the “End” point.

Yes that makes sense to me now that I look at the script, but what if my waypoints were jumbled up, and didn’t look like this
image
As you can see there all in order, what if they wern’t? I don’t think it would work

1 Like

Yea its ordering them based on whatever the Value of the value object is.

1 Like

is that what WayPointTable[value.Value] = point[i] is?

1 Like

I added some comments to help you see what’s happening at each step.
And I changed it to also ignore the name of the “IntValue” object.

local Waypoints = workspace.WayPointPath
local WayPointTable = {}

if Waypoints then
	--Grab any children of the WayPointPath instance
	local point = Waypoints:GetChildren()
	
	--Loop through each child in the table
	for i = 1, #point do
		
		--Find the "IntValue" child of this point
		local value = point[i]:FindFirstChildWhichIsA("IntValue")
		
		--If the "IntValue" object exists and its value is > 0
		if(value and value.Value > 0)then
			
			--Then put this point into the WayPointTable indexed by its value.
			WayPointTable[value.Value] = point[i]
		end
	end
	--Add the "End" point as last one in the table.
	table.insert(WayPointTable, (#WayPointTable+1), Waypoints:FindFirstChild("End"))
end
1 Like

Yes, so I was understanding it right I just wanted to know that I was right. Also, should I be using IntValues? I’m using number values

I don’t think it matters really. But if they are NumberValues just change it in the script to use NumberValues instead of IntValues

1 Like

I know you have found a solution now, but I am thinking that possibly you are not aware that :GetChildren() returns the order at which the instances have been inserted, rather than the order at which they have been sorted.
Maybe you were already aware of this?

1 Like