Simple way to use v in for i,v in pairs to access child of object

Hi all,
I was just curious if there was a way for v to access the child of an object. The code I provided below obviously doesn’t work, but I’m sure you can see what I’m going for here. So my main question is: Is there any way for me to get this code to work other than having to manually create if statements for all values of v? Thanks

for i,v in pairs(copy.Connectors:GetChildren()) do
	copy.Connectors.v.Depth.Value = copy.Connectors.v.Depth.Value + 1
end
1 Like

I don’t understand. Are you trying to get a child within copy.Connectors that has the same name as v?
Wouldn’t that be a little redundant? Just use v???

for i,v in pairs(copy.Connectors:GetChildren()) do
	local Depth = copy.Connectors:FindFirstChild(v.Name)
	Depth.Value += 1
end

You don’t need to use pairs() anymore so I think what you want is the following:

for _, child in copy.Connectors:GetChildren() do
	child.Depth.Value += 1
end
4 Likes

Are you sure? It doesn’t seem likely that you don’t have to use pairs()

all that pairs() does it list out the objects in order. If you just want to loop through them in any order it actually runs faster to not use pairs()

pairs does not list out objects in order, because a dictionary has no ‘correct’ order. The only way to parse a ‘table’ “in order” is by passing an array using ipairs. Using pairs otherwise does not guarantee that the table is parsed in the order that the developer writes it.

I didn’t mention dictionaries. I was more leaning towards regular tables that are preset.

It’s a fairly recent change iirc but you don’t.

You are mistaking pairs and ipairs. pairs has no guarantee for order, ipairs does.
Omitting either behaves the same as pairs.

with hashmaps, yes. It works just fine with regular tables

Yes I’m positive. The Luau announcements for May of 2022 added generalized iterators.

your code will not work, you should simply make:

for i, v in copy.Connectors:GetChildren() do
v.Depth.Value += 1
end