Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone explain to me what next() function is?

Can someone explain to me what next() is?

I see it in some scripts but I don't know what the hell it actually means.

1 answer

✓ Accepted answer

the next function, basically return the next object in a table according on the lastKey.

But, what is "lastKey" ?

its basically the second argument of the next function, if not defined it will return the first element from a table, for example:

local tbl = {'Hello', 'Second'}
print(next(tbl)) -- Hello
print(next(tbl, 1)) -- Second
print(next(tbl, 2)) -- nil (because there is nothing after the index "2")

you can use it to check if has a object in a table, or to do generic for loops, another example:

local tbl = {
	'Hello!',
	'This is a example of the "next" function!',
	':)'
}
for i, v in next, tbl do
	print(i, v)
end
--[[
Output:
  1 Hello!
  2 This is a example of the "next" function!
  3 :)
]]

Hope it helped! :)

1 comment

0

thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.