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
Hey,
I
andV
are 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 index
and the variable for v is value
.
The for loop is to iterate through a table
of items. Think of a table as a list of items.
You create a table like this:
local Table={"Variable", true, false, nil}
Any datatypes
can 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 IntValue
instance 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!
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.