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

When do we use for i,v in pairs and why? [closed]

Asked by 4 years ago

This question already has an answer here:

Can someone explain this "for i,v in pairs" to me?

I have seen people use for i,v in pairs in many places like tables and also in creating a round system but didn't get the answer how it is done and why only we use for i,v in pairs for on creating round system and many things i know that you can use it for tables but i don't know what is the more use of it as i see many people use it for diff rent things.

0
You keep reposting the same question, use the dev hub or find answers on youtube, dev forums etc. JesseSong 3916 — 4y

Marked as Duplicate by JesseSong

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 4 years ago

for i,v in pairs() is basically a for loop that loops through every item in a table. Although for i,v works just fine, for key,(word to help understand) works just the same, but in a more understandable way.

Anyways, lets say you wanted to unanchor all the parts in a model. Unanchoring every part individually would take a long time. for loops do that for you. Example:

local model = --model goes here

for key,obj in pairs(model:GetChildren()) do
    obj.Anchored = false
end

You can use a for loop whenever, like when a part is touched.

local model = --model again
local hitbox = model.Hitbox --You don't need a hitbox as you can call the function when any part is touched, it just helps

local function collapse()
    for key,obj in pairs(model:GetChildren()) do
        obj.Anchored = false
    end
end

hitbox.Touched:Connect(collapse)

I hope this makes things a lot more clear for you.

0
Just remember in an array with i, v i would be an enumeration of the position in the array and v would be the value at that position and in a dictionary i, v i would be the key and v would be the value for that key. msuperson24 69 — 4y
Ad