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

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

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?

3 comments

0

I've been having similar problems with the 'for' loop, it may be a problem with ROBLOX. :/

3

Are there actually any UnionOperations directly in the Workspace (not in models?) If you print(#unions), how many does it say there are?

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.

1 answer

✓ Accepted answer

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

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