How to get variables from an entire script source placed in a table?

I am making a plugin, on which the source of a script will be amplified.

How to get certain strings of code in an entire script source?

All i need is to get each variable in the source code, and placed in a table.
Let’s assume this is a part of the script containing variables:

-- Other part of the code....
local MyNumber = 12;
local MyOtherNumber = 13
-- Other part of the code....

How may i get each variable (Which is not IN a function), and place each of them in a table.
Like this:

local Table = {
MyNumber = 12,
MyOtherNumber = 13
}

Thanks.

You can use getfenv() for this means if I got it right

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.

You can, for example put code source you got into module script and add return getfenv() at the end, then require that script

I thought this only worked for global variables though. If so, then this won’t work on values that are local.

Use module scripts. Like this:

local variables = {a=1,b=2,c=3}

return variables

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.

3 Likes

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.