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.
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
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.
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
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.
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
As you can see there all in order, what if they wern’t? I don’t think it would work
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
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?