A quick rundown of how the script works: it's a simple tram which tweens along a path of waypoints. These waypoints are designated by a folder in the workspace titled: "Waypoints". In the folder there are a set amount of parts, each labled with a number such as "1" or "2". In my tram handler script, there is a for loop which checks through the folder and checks for which part is named the current waypoint plus one. Since when the tram initially begins it will be at waypoint 0, the current waypoint variable is declared as 0 and the for loop checks for waypoint 0+1 which is 1. After that loop cycles, it will check for 1+1, which is 2. Then, it should check for 2+1, but this is where the loop breaks. Completely breaks. The print function at the beginning of the loop doesn't fire so I know it just stops completely. I'm not sure why this has happened as it has worked fine before, and only broke after I renamed some of the parts. Yes, I did check for any whitespace in each of the waypoints' names but there's none. I'm really not sure what the problem is with it
#For loop breaking after two cycles
1 messages · Page 1 of 1 (latest)
activates mindreading powers hmm yes it's because you're getting an error, you forgot to carry the 1
in all seriousness though if you actually want help, describing the code isn't going to do anything because if your description of the code worked how you thought it did, you wouldn't be having any problems
Ya here i couldnt paste it cause it was too long
Oh alr i just saw the negative number i'll resend it
where is CurrentWaypoint and where is this function called from
because the problem looks rather simple
for _, waypoint in workspace:WaitForChild("Waypoints"):GetChildren() do
print("Searching waypoints: "..waypoint.Name)
if tonumber(waypoint.Name) == currentWaypoint + 1 then
currentWaypoint += 1```
the order of instance:GetChildren() is not guaranteed
and then depending on whether you call this function multiple times, it will reach the end and then just straight up stop completely and never loop
current waypoint is a variable at the top of the script
but yea pretty sure the issue is just the loop is skipping a bunch of points like it will look at point 3 then point 1 then point 2, and stop, because point 3 came before point 2 in the loop
and since you presumably only call it once, it will never find point 3
Well the loop doesn't even check the numbers once it gets to the second point i'll show u the output
your print should actually show this
It ends on the red one and doesnt even loop again to check for a third waypoint
It used to work completely but i switched the order of two waypoints and it doesnt work anymore
so yeah like i said, it's reading the later waypoints before the early ones.
it is exactly this
coz notice it searches here 3, then 4, then 2. and you're saying it stops at waypoint 2.
and since you presumably only call it once, it will never find point 3
i sure do love repeating myself 
So the loop won't recheck points it already checked through? i don't understand
code isn't magic
it does exactly what you tell it to do
and it's because you thought the order of GetChildren was guaranteed when it is not
you assumed getchildren would return a table of instances sorted by their name -- it's not sorted.
occasionally it may seem like it is sorted, but it is actually going by memory order. this is normal luau behavior
if you want the table sorted by name, use table.sort
alternatively, use for i=1, totalNumWaypoints, 1 do local waypoint=folder:FindFirstChild(tostring(i))
or something like that
you also apparently assume luau knows that it needs to loop the table more than once
both of these assumptions are incorrect
so ya hope that helps
reading is an ancient lost skill apparently
Hi Mr pyro fire,
Thanks for the fixes i appreciate it, especially getting talked down the entire time for asking a question in the question channel like im a sloth that had fetal alcohol syndrome but dont worry i enjoyd it as i have a severe degration knk 🤗
i'm not intending to talk down, only the part about not reading things closely
aside from that everything else is normal, like assuming getchildren is sorted by name when it isn't. common mistake for beginners, calling this an assumption is correct term
coz if you read your output window closely you'd have seen it checking later waypoints before the earlier ones, then cross reference that against what's written in the code and you see it only loops once, so it won't ever read the 3rd waypoint at a valid time
this is an assumption too but even if you called it more than once the behavior would still be erroneous from your first assumption that getchildren returns ordered result
don't worry though, you get used to checking stuff like this as you keep making stuff
you may think i'm making you feel stupid, but really it was just your code doing that 
very normal, makes me feel stupid all the time too