The problem of turning objects into strings is called serialization.
Serializing a table is difficult for the following reasons:
- Table keys aren't just numbers
- Tables can contain more tables
- Delimiters for entries are allowed in strings
- Tables can be recursive (
tab[1] == tab can happen)
If you use the RbxUtility library, you can use the EncodeJSON function to turn a table into a string, and the DecodeJSON to turn a string into a table.
JSON is a serialization method developed to support JavaScript, but it adapts very well to Lua objects (excepting that it cannot manage recursive entries or non-stringly keys)
If your tables are something simple, e.g., your table is only a list of words that you know never contain some particular character (like the newline "\n") then you can just put them together and then break it apart by those:
function wordListToString(list)
return table.concat(list, "\n");
end
function stringsplit(self, sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function stringToWordList(str)
return stringsplit(str, "\n");
end