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

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

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.

1 comment

0

You keep reposting the same question, use the dev hub or find answers on youtube, dev forums etc.

1 answer

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.

1 comment

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.

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