Whats does "next" in for loops even do?

What does

for _, v in next do 
--code 
end 

even do? what is the application of this? why not just use in table:GetChildren() do?
Did I use the correct format in the example?

It’s used in what’s called “iterator functions”. It gets the first (or next) key/value pair of the array passed. Iterator functions help us iterate over a table by handling logic and returning values.

You can use it by itself in a loop like so:

for k, v in next, {"x", "y", "z"} do 
	print(k, v)
end

Traditionally, you would just use an iterator function that uses next internally and returns these values for you, and one such function is pairs. With modern Luau, you have features such as generalized iteration, so you skip everything I just mentioned and simply do:

for k, v in {"x", "y", "z"} do 
	print(k, v)
end

Read more about it here and here. Read more about iterator functions here.

3 Likes

next takes in a table and the current index and returns the next index, value pair. You can use it for iteration like so:

but luau introduces generalized iteration so you shouldn’t use next/pairs for iteration anymore.

local tbl = { A = "A", B = "B", C = "C" }
for i,v in tbl do print(i, v) end
1 Like

To add onto what others have said, you can use next() to check if a dictionary is empty. Since #dictionary doesn’t work you would use next(dictionary) == nil to check if it is empty.

2 Likes

The for loop accepts iterator functions, but it will compile to a different thing entirely
Don’t do that
next itself just returns a next key
If no key is passed, then it returns the internal “first” key
There is also an “inext” function you can retrieve:

local inext = ipairs{}

But you shouldn’t be using them, really

Next does have use cases like:

Iterator for loop formats are:

Function,table, args…
Note that args PAST declaration will be equal to the return of a function

If I remember correctly
And functions get called, and all args are passed.

Fun fact:
ipairs and pairs upon call get turned into:

next,table

Also, a fun fact about inext:

It technically lets you start the loop not just from the 1st index
but from any
Unfortunately, ipairs doesn’t have such a possibility, so the only way is:

local tab = {1,2,3,4,5,6,7,8}

local inext = ipairs(tab)

for i,v in inext,tab,5 do--loop starts from 5th or 6th index (i don't remember exact logic)
  print(i,v)
end

Although don’t use function iterators; they are inefficient.

So:

for i,v in pairs({1,2,3,4}) do end

is equal to

for i,v in next, {1,2,3,4} do end

Logic wise
Compile wise if i remember there is SOME cases where its different
in modern luau dont use iterator functions though
just do:

for i,v in {1,2,3,4} do end
1 Like

How does next() work in the context of this code? Kinda confused

1 Like

it gets any key
the free stack is just with gaps array set to true in every key
Next simply gets a key
Because key here is either true or nil

So basically
free: {
1,2,3,4,10,4,20
}

Containing free keys
When key is freed its reassigned to free

so basically
free[11] = true–is free now

Idk random trick i figured out when making player id system for remote calls
Also when making player id you probably want 2 U8 with ID and gen id to prevent race conditions

Edit also more serious use would be:

2 Likes

Thank you all for your responses!

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