Scripting Helpers is winding down operations and is now read-only. More info→
Log in to vote

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

Asked by 9 years ago

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

You can see the [LuaLearner's lesson]( on those loops. Shawnyg 4330 — 9y
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. User#11893 186 — 9y
kk spyspace12 -7 — 9y

Locked by FunctionalMetatable and Leamir

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
Answered by
jobro13 980 Moderation Voter
9 years ago

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)

This will print all names of all children of workspace.