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

How can I read my saved data that is inside a table?

Asked by 4 years ago
Edited 4 years ago

Hello, I am currently working on a save script which works as planned but the only thing is, I keep getting a weird table looking thing. The data get correctly placed inside, but the table.insert is making a table go into another table. My goal is to be able to print the data inside the table successfully. Here is the table.insert part (this is in a for loop)

local data = {}

for i, v in pairs(workspace.Parts:GetChildren()) do
    table.insert(data, i, {
        ["Name"] = v.Name, ["Properties"] = {
        ["Anchored"] = v.Anchored; 
        ["CanCollide"] = v.CanCollide; 
        ["Transparency"] = v.Transparency; 
        ["BrickColor"] = v.BrickColor.Name; 
        ["Material"] = v.Material.Value; 
        ["Reflectance"] = v.Reflectance;

        ["X"] = v.CFrame.X; 
        ["Y"] = v.CFrame.Y; 
        ["Z"] = v.CFrame.Z; 

        ["R_X"] = v.Orientation.X; 
        ["R_Y"] = v.Orientation.Y; 
        ["R_Z"] = v.Orientation.Z
        }
    })
end

The end result is:

--[[

[{"Properties":{"R_Y":45,"R_Z":11.25,"Z":45.25,"Material":256,"Transparency":0,"Y":0.5,"BrickColor":"Medium stone grey","Anchored":true,"X":32.70001220703125,"Reflectance":0,"CanCollide":false,"R_X":-11.25},"Name":"Part"}]

]]--
(there is 1 part in the model "Parts" in workspace, therefore only getting the information from 1 part. If there were 2 parts in the model, the table would be 2 repetitions of the table.insert)

I am wanting to remove the "[{" and make it "readable" by printing, such as

-- EXAMPLE (above)

local dss = game:GetService("DataStoreService")
local playerslot1 = dss:GetDataStore("playersave1")
local player = game:GetService("Players").LocalPlayer

local datastore_data = playerslot1:GetAsync(player.UserId)
local player_data = httpservice:JSONEncode(datastore_data)

print(player_data["Name"], player_data["Properties"]["Material"])

To clarify, the example above is what I want the "reading" of the data process to be like, nice, short, straight to the point, and simple.

Any solutions appreciated!

1 answer

Log in to vote
0
Answered by
Farsalis 369 Moderation Voter
4 years ago
Edited 4 years ago

Using unpack(tbl, index, #list) should work.

0
It doesn't really work like that since the table will include multiple sets of data, keep in mind that the first table i provided (the green table) there will be more than that one chunk of data. It will insert the part data into the table for however many parts are inside the model "Parts" in workspace. vislbIe 4 — 4y
0
Ok, so what im getting, is that you get the first line of green text above. But dont want "[{" ? Farsalis 369 — 4y
0
I am wanting to remove at least one of the brackets so I can read the data. If the two brackets are left there, you can't access the table and you get nil in return. vislbIe 4 — 4y
0
Alright, have you tried using unpack? Farsalis 369 — 4y
0
I solved it myself, thanks for the help! The solution is to get the table and do "data.Name" for example, or "data,Properties.Transparency". vislbIe 4 — 4y
Ad

Answer this question