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

How do you access all of the contents of a table?

Asked by
Mowblow 117
9 years ago

I need to use the contents of my table and copy them. They are parts in the workspace, however I used

table.insert()

to add it to the table "unions". Here is my code:

unions = {}

function DefineForUnion() -- 
local things = game.Workspace:GetChildren()
    for i , v in pairs (things) do
        if v.className == "UnionOperation" then
            table.insert(unions,v)
            wait(2)
    end
    end
end

DefineForUnion()

for i, union in pairs (unions) do 
    print("We found the Unions!")   
    if (union.ClassName == "UnionOperation") then
            local new = union:Clone()           
            new.Size = union.Size * scale
            new.Parent = game.Workspace
            new.CFrame = CFrame.new(math.random(1,20),10,math.random(1,20))
            print("Copied a union...")
            wait(2)
        end

However it does not pass the print "We found the unions".

Any suggestions?

0
I've been having similar problems with the 'for' loop, it may be a problem with ROBLOX. :/ TheeDeathCaster 2368 — 9y
3
Are there actually any UnionOperations *directly* in the Workspace (not in models?) If you `print(#unions)`, how many does it say there are? BlueTaslem 18071 — 9y
0
None, however when I add prints to signify what parts of the script actually ran, it shows that the table.insert did run. And. all of the unions are inside the workspace, and are not in a model. Mowblow 117 — 9y

1 answer

Log in to vote
1
Answered by
nilVector 812 Moderation Voter
9 years ago

In order to loop through tables, you must use ipairs.

For example, your code should look something like this:

--Notice the use of "ipairs" here
for i, union in ipairs (unions) do
    print("We found the Unions!")   
    if (union.ClassName == "UnionOperation") then
        local new = union:Clone()           
        new.Size = union.Size * scale
        new.Parent = game.Workspace
        new.CFrame = CFrame.new(math.random(1,20),10,math.random(1,20))
        print("Copied a union...")
        wait(2)
    end
end
Ad

Answer this question