Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[solved]Is there a way to make a StringValue.Value into a table entry?

Asked by 8 years ago

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

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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 printed it out:

example[1] is "this", "is", "number1", which is just one string -- printing 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.

0
thx for all the info. i'm trying to make a free model that allows users who don't know scripting to be able to customize the model without editing any scripts. This method works if it only needs two infos, then i could make Name and Value into table entries. but alas, i need three infos for this particular project to function smoothly johnnygadget 50 — 8y
0
If you need three values, just make three StringValues. If you need something more powerful, do parse the string or use a module. BlueTaslem 18071 — 8y
0
Thx for advice. I was messing with trying to figure out parsing, then decided to try jsondecode and it worked! :D johnnygadget 50 — 8y
Ad

Answer this question