String to table

I used data store where each currency and everything was stored individually with their own key which wasted alot of space, made it slower and i reached the limit. i tried to shitch to tables now but i need to make the dictionary tables within them and not just normal ones as each item has some more configurations. I tried to turn a normal table into dictionary with given name but i coudnt find a way to do it. anyone know how to do it?

1 Like

Assuming both keys and value can become string, You can turn a dictionary to string by running

local function dictionaryToString(dictionary)

local result = ""
for key,value in pairs(dictionary) do

local newKeyValPair = tostring(key) .. "," .. tostring(value)
result = result .. "|" .. newKeyValPair

end
return result

end

to convert this back into a dictionary, you can do

local function stringToDict(StringInput)

local result = {}
local keyValPairs = StringInput:split("|")
for i,keyVal in ipairs(keyValPairs) do
keyVal = keyVal:split(",")
result[keyVal[1]] = keyVal[2]
end
return result

end
uhhhh
local Colors = {
    RED = "\27[31m",      -- for numbers
    GREEN = "\27[32m",    -- for booleans
    YELLOW = "\27[33m",   -- for strings
    CYAN = "\27[36m",     -- for keys
    RESET = "\27[0m"
}

local function colorize(value, color)
    return string.format("%s%s%s", color or "", value, Colors.RESET)
end

local function serialize(value, indent)
    indent = indent or ""

    local t = type(value)

    if t == "string" then
        return colorize(string.format("%q", value), Colors.YELLOW)
    elseif t == "number" then
        return colorize(tostring(value), Colors.RED)
    elseif t == "boolean" then
        return colorize(tostring(value), Colors.GREEN)
    elseif t == "table" then
        return visualize(value, indent)
    elseif t == "nil" then
        return colorize("nil", Colors.GREEN)
    else
        return colorize("<"..tostring(value)..">", Colors.YELLOW)
    end
end

function visualize(tbl: {}, indent: string): string
    indent = indent or ""
    local gap = "    "

    if not next(tbl) then
        return "{}"
    end

    local result = "{\n"

    for key, value in next, tbl do
        if type(key) == "string" then
            key = colorize(key, Colors.CYAN)
        else
            key = "[" .. serialize(key, indent..gap) .. "]"
        end

        result ..= string.format("%s%s = %s,\n", indent..gap, key, serialize(value, indent..gap))
    end

    result ..= indent .. "}"
    return result
end

return function(tbl: {}, ...)
    local res = visualize(tbl)
    print(res, ...)
end

i use this in VSCode when I want to view tables, don’t know if studio supports the colors, probably not so just remove the colorize function

nvm this is not what u were looking for but imma keep this incase anyone else needs this

so there is the main table that is saved, within there are subtables that have more values, something like this i want to archive:
[“tableThatSaves”] = {
------[“Currency1”] = {
------------[“Owned”] = 20,
------------[“Value2”] = 1,
------},
------ [“Currency2”] = {
------------[“Owned”] = 15,
------------[“Value2”] = 2,
------},
}

i have the parts
[“Currency2”] = {
------[“Owned”] = 15,
------[“Value2”] = 2,
},
done but not as dictionary rather normal table defined as
local tableName = {
------[“Owned”] = 15,
------[“Value2”] = 2,
},

and i need to turn the preset savetable
[“tableToSave”] = {
------ “Currency1”,
------“Currency2”,
}
to connect via changing strings in it to coresponding dictionary tables

basicly turn a String within a table into an dictionary table whose name is the same as string with already pre-defined content which another normal table has

Not sure if this could work, but call the function recursively?

local dictionaryToString
dictionaryToString = function(dictionary)

local result = ""
for key,value in pairs(dictionary) do

local toJoin = ""
if typeof(value) == "table" then
task.wait() -- prevent timeouts
toJoin = dictionaryToString(value)
else
toJoin = tostring(key) .. "," .. tostring(value)
end

result = result .. "|" .. toJoin 

end
return result

end

thank you for the reply, however i did on end menage to find a way to go around this, not that i like it too much but it works

i just kept it saved by index numbers as i do already have them pre-stored so i called the tables by indexes and not the names

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.