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

How to add Object to Table?

So I made a for loop that goes through Workspace and checks if there are any Object that have a ClassName called Part.

for i,v in pairs (game.Workspace:GetChildren()) do
if v:IsA("Part") then
--------------------------
--Insert Code Here--
--------------------------
	end
end

Here is the Table where I want the Part to be added. So I got the condition set up but I want to know how can I Part to my Table.

PartList = {}

Thank you for taking the time to read my unusual question, I would appreciate it could help! :)

4 comments

0

Title meant to say "Objects" not "Object".

1

Why is there a "end)"? Should be "end"

0

Please do not mark this question as duplicate. There is an explicit difference between appending to a list and adding a key to a map. Although they are similar questions, they are very different things.

0

@TheContinentoEurope That was a silly mistake ignore it :s

1 answer

✓ Accepted answer

If you want to insert a new entry into a list, which is a type of table with numerical keys, the easiest way to do that is to use table.insert. Here is an example...

local t = { } -- our table
table.insert(t, "a") -- insert "a" into t
print(t[1]) --> "a"

So, in this case, we just use that technique for your code. It should look something like this.

local PartList = { }
for _, object in next, workspace:GetChildren() do
	if object.ClassName == "Part" then
		table.insert(PartList, object)
	end
end

1 comment

0

Thanks Sukinahito! :D

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