Accessing the Xth element in a for iv loop

I’ve got a small question because I was testing it out in studio and it didn’t seem to work.

Let’s say we have a model and we are using an i,v in pairs loop and I just want to just access the part in the 4th position in the model. How come when i do v[4] i get an error saying that 4 is not a valid member of the model.

So the question is, in a for loop, how would you just call the Xth element of a table.

You mean you used something like this?

for _, v in pairs(model:GetChildren())
    -- this is how you loop through a model, if index isn't used
end

What you mean by 4th position depends. Either you do model:GetChildren()[4] as literal as it is, or it has to be a specific part, which needs the looping.

1 Like

Yeah a loop like that, however I only want to call for example the element in the 4th position of the model because i just want to change that part’s properties

for index, v in pairs(model:GetChildren()) do
    if (index == 4) then
        v:Destroy() -- example
    end
end

reminder that roblox array index starts with 1

I know that in a table, if you do smth like:

print(tableName[4].Name)

then all it would output is the element in the 4th position.

Why can’t I just do this:

print(v[4].Name) --Let's say we already wrote the for i,v loop

i dont understand

your for loop is looping thru a model, you cant index an instance with a number (v[4])

Whenever the table is populated by the model:GetChildren(), the order of parts are completely randomized and unreliable to always pick the 4th part by position. You can otherwise set that specific part as Model.PrimaryPart and then always index model.PrimaryPart(specific case) to get that said part.