Hey!
I’ve tried to store multiple informations about table key in one attribute and found one very hacky way and I would love to see if there is any better solution for this kind of thing at the moment.
Here’s my example code:
local Fold = workspace.Terrain.Folder
local Table = {
["Example1"] = {Level = 5,Exp = 10},
["Example2"] = {Level = 10,Exp = 5},
}
local Tbl2 = {}
for i,v in pairs(Table) do
for x,k in pairs(v) do
table.insert(Tbl2,{Name = x,Value = k})
end
Fold:SetAttribute(i,string.format("%s %s %s %s",Tbl2[1]["Name"],Tbl2[1]["Value"],Tbl2[2]["Name"],Tbl2[2]["Value"]))
end
Thanks for any possible improvement, reading and lets hope that array attributes will be a built-in thing.
Yeah I can see how this would be problematic especially when it comes to strings with spaces and having to parse the attribute values later.
I’m pretty lazy but it lines up well with my love for attributes so in my case I’d probably be storing each table entry as its own Folder with each of the indices being their own attribute. Probably could get tricky for multidimensional tables though. For player data incorporating attributes though there’s no argument and I would definitely store data as separate folders, not all in one.
My next lazy (or maybe not?) solution would to use JSONEncode. The nice thing is that this would hypothetically allow you to store multidimensional tables in one attribute. The JSON format would strictly be for attribute use - you encode before you set and you decode after you get.
local HttpService = game:GetService("HttpService")
local Folder = workspace.Terrain.Folder
local Table = {
["Example1"] = {Level = 5, Exp = 10},
["Example2"] = {Level = 10, Exp = 5},
}
for label, data in pairs(Table) do
Folder:SetAttribute(label, HttpService:JSONEncode(data))
end
-- When you fetch the attribute's value (assuming you know for sure
-- that any attribute's value will always be a JSON-encoded string)
local data = HttpService:JSONDecode(Folder:GetAttribute("Example1"))