What you want now is a table to string function. You can use JSON; it outputs quite readable tables, or you can make your own quick table-to-string function.
Here’s the latter approach:
[lua]
– This function converts a table to a string representation
local function tableToString(t)
local str = {} – It’s a table because then we’ll concatenate it together and it’s faster than doing it to a string
for k, v in pairs(t) do
table.insert(str, k … " = ")
local vType = type(v) – Take the type of ‘v’ so we can know how to change it into a string
if vType == “string” or vType == “number” then
table.insert(str, v) – It can be added really simply
elseif vType == “boolean” then
table.insert(str, (v == true and “true”) or “false”) – Convert Boolean to string
elseif vType == “table” then
table.insert(str, tableToString(v)) – Convert it to a table before adding it
end
end
return “{” … table.concat(str, ", ") … “}” – Concatenate the key/value pairs with commas
end
– Now for the loop
local prefix = “lg_” – What you want global variables to be found to start with
local prefixLength = prefix:len() – Just an optimization
for k, v in pairs(_G) do
if k:sub(1, prefixLength) == prefix then – Do whatever; it matches the prefix
local vString = v – What we’ll print out
local vType = type(v)
if vType == “string” or vType == “number” then
– Nothing to do here
elseif vType == “boolean” then
vString = (v == true and “true”) or false
elseif vType == “table” then
vString = tableToString(v)
end
print(k … " : " … vString)
end
end
[/lua]
And here’s the former:
[lua]
local json = require(“json”) – Include the JSON library
local prefix = “lg_” – What you want global variables to be found to start with
local prefixLength = prefix:len() – Just an optimization
for k, v in pairs(_G) do
if k:sub(1, prefixLength) == prefix then – Do whatever; it matches the prefix
local vString = v – What we’ll print out
local vType = type(v)
if vType == “string” or vType == “number” then
– Nothing to do here
elseif vType == “boolean” then
vString = (v == true and “true”) or false
elseif vType == “table” then
vString = json.encode(v) – Transform the table into a JSON string
end
print(k … " : " … vString)
end
end
[/lua]