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

Way to insert dictionary into a table?

Asked by
uhSaxlra 181
4 years ago

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

Log in to vote
1
Answered by
B080 19
4 years ago

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.

Ad

Answer this question