How can i use that tho? I am in meaning of a raw text containing variables, and willing to set the string of code containing the text alike a variable (Full Variable: local a = 12;) into a table.
Then you can use require() in a script to get these variables like this:
local variables = require(game.Path.To.Your.ModuleScript)
print(variables) --Should return a table unless there is a syntax error
Another thing you can do is that you can create values that are parented to the script that have a variable’s value assigned as the value instance’s value.
Like this:
local a = 52
local b= "hi"
local variables = {a,b}
for _,v in ipairs(variables) do
if type(v) == "number" then Instance.new("NumberValue",script).Value = v end
if type(v) == "string" then Instance.new("StringValue",script).Value = v end
end
If the scripts are meant to be edited by the user to suit your plugin, then go ahead and run the script by either requiring it as a modulescript (the user must return the values in a table) or by loadstring()ing the script (the user must return the values, or only use globals so that they show up in getfenv, then return the env)
If the scripts are not safe to run or not specifically tied to your plugin, then unfortunately you have to parse the script in some way.
The dummiest way to do it is to search for assignments in text.
local source
for key, value in source:gmatch("(%a%w*)%s+=%s+([^\n]+)") do
print(key, value)
end
This might find foo = 123 or aaa12313123 = "asdadasd", but also test = $^##$@?# and nil = while true do end. It does not find 123 = 321 because the name must begin with a letter. It will also consider any trailing ; to be part of the value and will choke on multi-assignments (will only find jkl = 1, 2, 3 from asd, fgh, jkl = 1, 2, 3) and will obviously not calculate five = 2 + 3.
If you want any better than this, you will need an actual Lua parser, and a little runtime for it (to resolve things like 2 + 3 or variable references), which is complicated to do.
What you’re describing depends on scope. Hopefully you only mean the initial scope and not within a function or loop or anything. In this case, you can use String Patterns to get the variables from the Script.Source. In this case, I would use string.gmatch to loop through all matches with the pattern %w+%s*=.*%w-.
EDIT: Nevermind. I’d use @Eestlane771’s pattern instead.