Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 6 years ago

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

0
You can see the [LuaLearner's lesson](http://lessons.lualearners.org/lesson?id=86) on those loops. Shawnyg 4241 — 6y
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. User#11893 186 — 6y
0
kk spyspace12 -7 — 6y
0
this question isnt locked, so i can leave a comment! theCJarmy7 1276 — 4y
0
@theCJarmy7, you and I both felt the need to see what the 100th question on SH was xD. Nothing important. User#11440 109 — 4y

1 answer

Log in to vote
3
Answered by
jobro13 980 Moderation Voter
6 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)
end

This will print all names of all children of workspace.

Ad

Answer this question