Let’s say I have a StringValue with it’s value being “{1,2,3}”, how do I turn the value into a table I could index with table[1]?
The fact that you’re getting the string from a StringValue object isn’t important, but one method for converting that string into a table would be with loadstring;
local s = "{1, 2, 3}"
local t = loadstring("return "..s)()
but loadstring is disabled on the client and is easily exploitable if you are careless about how you use it. Instead, parsing the string is another option:
local t = {}
for v in s:gmatch("%d+") do
t[#t+1] = tonumber(v)
end
this will iterate through grouped strings of numerical characters and add the numbers to a table.
I think clonetrooper1019 made a module for this. Gimme a sec…
Edit: Here we are:
https://www.roblox.com/library/91426212/Table-to-String-String-to-Table
Couldn’t you just JSONEncode and JSONDecode to write and read from the table value?
Sticking it directly inside of your script would be the easier route, but it is understandable if that isn’t possible. We’ll have to parse the table. That has varying levels of difficulty depending on how complete you want your parser to be. To parse a string that is always of the format 1, 2, 3, ...
you can use @1waffle1’s example. clonetropper1019’s script does about the same thing.
But if we had a more complex structure, we would want a full-fledged parser. Using my GLR parser I changed the input file to
{1, 2, 'a', "b",
c = 3;
['d'] = {};
e = false;
f = '4',
[{}] = {
5, 6;
};
}
(A difficult table to parse)
And I made made a syntax and semantics file:
/grammar/tableParser/syntax.lua
return function(settings)
local syntax = settings.require('productions/Syntax').define()
local e
START = TABLE * eof
local FIELD_SEP = delimiter[','] + delimiter[';']
TABLE = delimiter['{'] * (FIELD_LIST + e) * (FIELD_SEP + e) * delimiter['}']
FIELD_LIST = FIELD + FIELD * FIELD_SEP * FIELD_LIST
FIELD = VALUE
+ KEY * delimiter['='] * VALUE
KEY = variable
+ delimiter['['] * VALUE * delimiter[']']
VALUE = keyword['nil']
+ keyword['true']
+ keyword['false']
+ number
+ delimiter['-'] * number
+ String
+ TABLE
return syntax
end
/grammar/tableParser/semantics.lua
return function(settings)
local semantics = settings.require('grammars/tableParser/syntax'):extend()
function variable(t, o)
o.value = t
end
function String(t, o)
o.value = t
end
function number(t, o)
if o.isNegative then
o.value = -tonumber(t)
else
o.value = tonumber(t)
end
end
function keyword(t, o)
o.value = t == 'true' and true
or t == 'false' and false
or nil
end
function delimiter(t, o)
if t == '-' then
o.isNegative = true
end
end
function TABLE(f, o)
o.value = f{}
end
function KEY(f, o)
local key = f{}
o.key = key.value
end
function VALUE(f, o)
local value = f{}
o.value = value.value
end
function FIELD(f, o)
local field = f{}
if field.key then
o[field.key] = field.value
else
o[#o + 1] = field.value
end
end
function FIELD_LIST(f, o)
f(o)
end
return semantics
end
And my parser was kind enough to spit out a table with this structure:
{
value = {
1 = 1;
2 = 2;
3 = "a";
4 = "b";
[{}] = {
1 = 5;
2 = 6;
};
d = {};
c = 3;
f = "4";
};
}
Send me a PM if you are interested in a free model of my parser. Right now on GitHub it is setup to run outside Roblox in an OS with Lua installed, but I’ve added some configuration files that will make the transition to Roblox easy. I’m working on an IDE for it right now.
Of course! I completely forgot about JSON.