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

I want to know whats I and v in roblox and what does iv in pairs do and how it can be used?

Asked by 8 years ago

I simply want to have a explanation of what is "I" and what is "v" in roblox scripting, and what does iv in pairs do and how could it be useful

Thanks

2 answers

Log in to vote
3
Answered by
Thetacah 712 Moderation Voter
8 years ago
Edited 8 years ago

Hey,

I andVare just standard variables we use in a forloop.

I and V can be named anything you want, just know that the variable for i is indexand the variable for v is value.

The for loop is to iterate through a tableof items. Think of a table as a list of items.

You create a table like this:

local Table={"Variable", true, false, nil}

Any datatypescan be stored in tables.

We then use the for i, v in pairs loop to iterate, in other words "Go through" the list of items.(Table)

This is done like this:

local Table={"Variable", true, false, nil}
for i, v in pairs(Table) do
    print(i, v)
end

That code would print its Index: Number of the item that's in the table and the value: The value, being Variable, True, false and nil

Whats it usefull for? Well, going through tables....

One use could be:

local players=game:GetService("Players"):GetPlayers()

for i, v in pairs(players) do
    Instance.new("Intvalue", v)
end

That code would iterate through all of the players of the game and insert a IntValueinstance inside the player.

As I said, i and v could be named anything so this code would work just as well :


for index, value in pairs(game.Workspace:GetChildren()) do print(index, value) end

Have you ever wanted to have the abilty to delete all of the parts in the workspace? You can accomplish just that using a for loop.


local parts=game.Workspace:GetChildren() for i, v in pairs(parts) do if(v:IsA("Part")) then v:Destroy() end end

To conclude, there are many uses for generic for loops and I suggest you experiment with this and hopefully become a for loop expert!!

Cheers!

Ad
Log in to vote
0
Answered by 8 years ago

These are just names of the values in a for loop, index and value which is usually the number id of the value which is the value that it's returning.

0
Um i dont really understand, so i is just a ordinary variable or is it neccesary for this type of loop? y0ucef123 72 — 8y
0
Both. DeveloperSolo 370 — 8y

Answer this question