so, i'm trying to make tables within tables, easy enough, but i'm trying to make the nested tables from stringvalue.Value. i made a StringValue.Value = ["this","is","a","table"] then used jsondecode to make it act like a table instead of just a string.
local pretable = StringValue.Value newtable = game:GetService("HttpService"):JSONDecode(pretable ) print(newtable[1]) print(newtable[2]) print(newtable[3]) print(newtable[4]) output:.............................................. this is a table
You are misunderstanding how strings work.
In this line
example[2] = {"this", "is", "number2"}
Lua reads it bit by bit. It sees, {
, so it knows it's making a list.
It sees "
, so it knows it's starting a string. The second "
lets it know that this
as text is the first string. Then a ,
, so it knows it's starting another thing in the list. Etc.
When it see this line:
example[1] = {script.StringValue.Value}
it sees, {
, so it's starting a list. Then it sees this first and only thing in that list, script.StringValue.Value
, so it grabs that value, and puts it in the list.
When you write code in a string, Lua does not run that code.
It would be a nightmare if it did -- you wouldn't be able to make strings with, say, "3 + 4"
because it would immediately change itself into 7
. What would you even do if you had {"this"
in your StringValue? Do you just get an error for even typing that because it doesn't close the curly brace? When you chat that everything breaks because it can't handle?
The contents of the string value are the string -- and not a list.
You can see exactly that when you print
ed it out:
example[1]
is "this", "is", "number1"
, which is just one string -- print
ing omits the quotes around strings.
You probably don't want to actually write your StringValues as though they were tables.
If you do, you should just use a ModuleScript that returns that table, rather than a StringValue.
If you want to have some structured information in a string... then come up with a simple format for yourself. E.g., if you want every word in a string, you can write that using gmatch
:
local text = "This is a string!" for word in text:gmatch("%S+") do print(word) end -- This -- is -- a -- string!
If you need to stick with StringValues, and you have exactly three values... Just make 3 different StringValues! And give each the name that describes what it is!
That's much more friendly than a weird format in one string value.