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

What's the difference between a numeric for loop and a generic for loop?

Could you please explain the difference between a numeric and a generic for loop?

3 comments

0

You can see the LuaLearner's lesson on those loops.

0

Spyspace12, please leave a comment or append an edit to the end of your post. Do not remove your post or add an answer. If you want to say "Thanks", simply click the Accept Answer button.

0

kk

1 answer

Basically, a numeric for loop loops over a certain array of numbers with a certain increment. For example, for x = 1,5,2 do print(x) end will print 1, 3 and 5.

A generic for loop is another type of for loop. You basically use the for loop as iterator again, but now you will use a function to get the values. One of the most used functions for a generic for loop is pairs(). This will slide a table index and it's value to the values;

local tab = game.Workspace:GetChildren()
for index, instance in pairs(tab) do
print("Instance number: "..index.." of game.Workspace has name: ".. instance.Name)
end

This will print all names of all children of workspace.

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