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

How would I use a For loop to refer/do something to each item in a table?

Asked by
drew1017 330 Moderation Voter
8 years ago

So, I have a table that has 3 Vector3 values. Im trying to create a part at each of the 3 values, and to do that I obviously need a for loop. However, I don't have too much knowledge on tables and variables, so could someone tell me how to properly do this?

2 answers

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
8 years ago

First you would need to set up your loop. To do this, we simply need a statement, and an end:

for i,v in pairs(Table) do
    --Code
end

The i will print out the number of the table that it currently is iterating over, and the v will print what is at that position in the table. So let's say I add this in:

Table={"Apple","Banana","Cheese"}

for i,v in pairs(Table) do
    print(i..": "..v)
end

It would go ahead and print out:

1: Apple

2: Banana

3: Cheese


So to set up a loop for your situation is quite simple. You would need a table of the Vector3 values, and a for loop:

VectorValues={} --Add them in here

for i,v in pairs(VectorValues) do
    local Part=Instance.new("Part",game.Workspace)
    Part.CFrame=CFrame.new(v)
    --Customize the Part however you like here
end

So this would create as many parts as there is VectorValues in the Table. To put the values in correctly, makes sure you have them in this format:

Ex.

VectorValues={Vector3.new(0,10,5),Vector3.new(5,12,5),Vector3.new(2,5,6)}

Ok, so anyways, this should work. Hopefully I helped clear things up for you a bit. You should also note however, that this isn't the only way to use a for loop. More on that here.

Anyways, hope I helped, if you have any further questions or need some explanation, please leave a comment below.

0
So if the table of Vector3s were created by PathfindingService, would this work as expected? drew1017 330 — 8y
0
I'm not really farmiliar with that, but I believe that it would print out the entire vector, so let me go ahead and update my answer. dyler3 1510 — 8y
0
I think it would work properly now. If not, let me know, and i'll see what I can do. dyler3 1510 — 8y
0
get on chat drew1017 330 — 8y
Ad
Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

What you're looking for is called a generic for loop.

You may be familiar with this construct:

for _, v in pairs(...) do
    ...
end

This is what we will be using.

--First, get the Table of Vector3s:
local vectors = {Vector3.new(), Vector3.new(1, 25, 6), Vector3.new(52, 17, 6)}

--Prototype a Part:
local part = Instance.new("Part")
part.FormFactor = Enum.FormFactor.Custom
part.Size = Vector3.new(.4,.4,.4)

--And finally, our loop:

for index, value in pairs(vectors) do
    --index is the 'key' of the Table. For this table, it will be 1, 2, or 3.
    --value is the 'value' of the Table at the current 'key'.
        -- aka: value = vectors[index]

    local clone = part:Clone()
    clone.Position = value
    clone.Parent = workspace
end

Answer this question