How do I convert this string into a table?

Before you guys start yelling at me, yes. I did try the search and I have found answers, but they all seem to be for standard tables that don’t seem to work with mine. The problem with my array, is that it isn’t a proper array. It also has another set of arrays within the list.

How can I convert this string into a functional table?

Some help will be greatly appreciated as always!

local TextToConvert = "{1,1,1},{0,0,0},{1,1,1}"

-- What I want the end result to be:

local Table = {
    {1,1,1},{0,0,0},{1,1,1}
}
print(loadstring("return {{1,1,1},{0,0,0},{1,1,1}}")())

worked perfectly fine for me (I did not test it in studio, I tested it in an online lua compiler). In order to adapt this method to your code:

local textToConvert = "{1,1,1},{0,0,0},{1,1,1}"
local tbl = loadstring(("return {%s}"):format(textToConvert))()

print(tbl)
6 Likes

The above method does indeed work in Studio.

There is also the JSON De/Encode methods apart of the HTTPService if you want to send it to an external service.
JSON Format (roblox.com)

1 Like

Wow, this is exactly what I was looking. Can you tell me what these functions and pattern do that you have used in your code?

Such as 'loadstring(), :format(), and %s'

I’m really bad at explaining so bear with me :slightly_smiling_face:

loadstring is an inbuilt method in lua, which basically returns a function containing the code string you provided in its argument. For example:

loadstring("
   local returnValue = 'Hello World!'
    return returnValue
")

would return a function with a similar structure:

function()
    local returnValue = 'Hello World!'
    return returnValue
end

If you call the function, it would return Hello World!, because that’s what the code is like.

string:format(parameters) is basically just the short way of string.format("string", parameters). It’s basically like concatenation but better. I don’t really know how to explain this…

References because my explanation is bad:
loadstring: Lua Globals | Roblox Creator Documentation
string.format: Strings | Roblox Creator Documentation

2 Likes

This would have been great to know before I started on my file data loader. Thanks!

i’ll give you this, your explanations are better than mine. I spent most of my time making this post from me trying to explain what I wanted to achieve. Sometimes I don’t even get past the requirements to post a bug report. But thanks for your help anyways.

Happy developing!

1 Like