Hello Developers ! So I have been watching and learning through out Alvinblox’s tutorial on YouTube about “for iv in pairs” I still don’t understand what is it used for or how does it work because I’m kinda puzzled.
Sorry if I don’t have much statement/question on my topic to cover with.
I’m just a “Noob scripter” asking for help on devforum so I could get a better understanding and learn from you guys of the code itself. Thanks
i is referencing index and v is referencing value, however, you do not always need to name it i and v, it could be _ and w.
local Table = {1,2,3,4,5}
for i, v in pairs(Table) do
print(i) -- The output will be 1,2,3,4,5 because there are 5 positions in the table.
print(v) -- The output will also be 1,2,3,4,5.
end
You could also get items from a folder:
for _, v in pairs(Folder:GetChildren()) do
if v.Name == "HelloThere" then -- v would be the items in the folder, so that could be a Part, Model, Another folder.
print("here")
end
end
Yea so I definitely recommend you searching before posting because there are a bunch of tutorials explaining the i, v in pair loop. Not just Alvin_Blox.
Can be used for alot of things, for example if you want to loop through something, for example if you want to track if a player is near something, you can loop through the wokspace.FolderName:GetChildren() and then use basic scripting from there if you would like to learn more info check out https://developer.roblox.com/en-us/articles/Loops
I’m pretty sure you have to print v … “has been destroyed.” first, as it would be nil after it got destroyed (correct me if I’m wrong).
For the OP; it’s basically just something that loops through a table;
local myTable = {
"Hi",
"Lol",
"Idk",
"Idk2"
}
for i,v in pairs(myTable) do
print(tostring(i) .. " - " .. v)
end
i basically stands for index and v stands for value. You can change both of them if you want to, basically just like variables.
A little bit more advanced: If it’s an Array, use ipairs instead of pairs so it loops through it in the correct order (an array is something like this: local myArray = {“Hi”, “lol”, …} and a dictionary something like this local myDictionary = {indexName = “Something”, indexName2 = “Something2”})
local myArray = {
"Hi",
"Lol",
"Idk",
"Idk2"
}
for i,v in ipairs(myArray) do
print(tostring(i) .. " - " .. v)
end
When printing out i (a number) right before/after v (a string, in our case), you have to call the tostring() function on i, which basically converts the number to a string correct me if I’m wrong.
In Roblox, “IV” stands for “Item Value.” It is a property of an item that represents its value or rarity. In a pair of parentheses after “IV,” you might see a number or a variable. The number or variable refers to the IV of a specific item.
For example, if you have a function called “GetIV” that takes an item as an argument and returns its IV, you might see code like this:
local item = game.Workspace.Item
local itemIV = GetIV(item)
In this code, the variable “itemIV” will be set to the IV of the item represented by the “item” variable. The value of “itemIV” might be a number, a string, or some other data type, depending on how the “GetIV” function is implemented.