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

Way to insert dictionary into a table?

So what I'm trying to do is save a block's data to a dictionary, then add that dictionary to a table. Then on, I could save it to the client's datastore.

What I'm dealing with:

Whenever I do this I get an error stating that the table data is nil.

Script:

local objects = workspace.Objects
local data = {}

for i,v in pairs(objects:GetChildren()) do
local bu = {
	Name = "Box",
	PosX = v.Position.X,
	PosY = v.Position.Y,
	PosZ = v.Position.Z,
	Rot = v.Rotation.Value
}
table.insert(data, bu)
end

I don't know why, but I know I'm doing something wrong.

1 answer

✓ Accepted answer

Your code does indeed store the bu tables into the data table, but they can't be referenced because it lacks key indices and hashes. Because you can't reference them, they become nil when you try to.

You can either use a for loop and just iterate through the "data" table to get its elements like this,

for i, v in pairs (data) do
	print(v)
end

or you can replace

table.insert(data, bu)

in the existing loop with

table.insert(data, i - 1, bu)

to specify the index of the table.

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