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

What is "for i, v in pairs() do". Why do you use it? Why is it useful?

I look on scripts and seen that there is a part of code saying:

for i, v in pairs() do

end

What does it mean? Where do you use it? Why is it useful? I am wondering why.

I know already about tabels. What does this relate to tables?

3 comments

0

Its a loop that loops over the children, descendants of an object or table.

0

Thank you that was a little interesting! Hope I could get a little more information from others!

2 answers

✓ Accepted answer

You're friends wanted to party with you in your game, and you decided to make a dialogue.

Okay, let's say you have dialogue, in the dialogue you want some GUI's. Let's say you had 20 GUI'S, you want to make them all visible. Instead of writing long repetitive script, and make them all visible we use

In a nutshell it loops through the descendants.

for i, v in pairs() do

You can name them whatever you want, they will still have the same value fx.

for _, Gui in pairs() do

We can insert some parameters in the parenthesis, to simplify it we need to insert what we want it to loop in.

In this case we want to make the GUI's visible. Let's try something simple;

wait(2)

for _, Gui in pairs(script.Parent.Folder:GetChildren()) do
Gui.Visible = true

You can do this in any case! Very useful.

Let me give you some examples

EXAMPLE1;

wait(4)

for i, part in pairs(game.workspace.Folder:GetChildren()) do
part:Destroy()

EXAMPLE2;

for i, v in pairs(script.Parent.Folder:GetChildren()) do
v:Play()
wait(5)

And also for future reference when you put a wait below the loop, it waits that amount of seconds before it does it again like here:

for i, v in pairs(game.workspace.Folder:GetChildren()) do
v:Destroy()
wait(4)

So here it destroys every part in the folder every 4 seconds, get it?


CONCLUSION:

You have learned about how to loop through folders, and models.

You have learned when to use the loop.


If this seems hard, you may want to learn about loops and repeat loops!

Repeat loops and while loops click here

Aside from all that, have a wonderfull day and stay safe.

2 comments

0

Thank you for helping me out a little bit more!

0

No problem! I'm always willing to help.

It's called an enhanced ?for loop. The i, v is defining variables for each index and value of the table that you are iterating through. k for key is often used rather than i in most cases. pairs is your iterating function, it returns each index and value of the table. Another not so common one is ipairs, which functions the same for the most part except that it will stop the loop once it runs into a non-existent (nil) value. For example:

local myTable = {5, "pineapple", [23] = 6, ["foo"] = true}
for key, value in pairs(myTable) do
    print(key, value)
end
1       5           -- because we didn't define a key for the value 5, it defaulted to index 1.
2       pineapple   -- same case here, except they index is now 2.
23      6           -- we defined a key by using square brackets and we set the index to 23.
foo     true        -- keys can also be strings, and we can store any value we want in a table.

As noted by : profile Also useful source : roblox wiki

1 comment

0

Interesting to know. I got to know some information from here and I'm going to research a little more about this. But thank you for helping me to learn this. I mabye didin't get from the first but then I got the point. Sincerely, Ducksneedhelp

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