#For loop breaking after two cycles

1 messages · Page 1 of 1 (latest)

cursive barn
#

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

final grove
#

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

cursive barn
#

Ya here i couldnt paste it cause it was too long

final grove
cursive barn
final grove
# cursive barn

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

cursive barn
final grove
#

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

cursive barn
final grove
#

your print should actually show this

cursive barn
#

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

final grove
final grove
final grove
#

i sure do love repeating myself Thumbs

cursive barn
final grove
#

it does exactly what you tell it to do

final grove
#

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

final grove
#

both of these assumptions are incorrect

#

so ya hope that helps Thumbs reading is an ancient lost skill apparently

cursive barn
final grove
#

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

final grove
final grove
#

don't worry though, you get used to checking stuff like this as you keep making stuff Thumbs you may think i'm making you feel stupid, but really it was just your code doing that hehe

#

very normal, makes me feel stupid all the time too