As a Roblox developer, it is currently too hard to view the contents of a table or dictionary in-game by printing it to the output.
The Studio output will display the contents of a table:
But the in-game developer console only prints the memory address, which isn’t helpful for debugging:
If Roblox is able to address this issue, it would improve my development experience because I would be able to find issues in my games much more quickly.
I actually made a function that prints a table input as a string itself, meaning it can be displayed in other places as well:
--[=[
Converts a table to a string, useful for displaying tables on GUI's. This is compatible with nested tables, dictionaries, and arrays. Example:
```lua
local MyTable = {"This table", "was converted into", 1, "string", "!"}
print(Utility.TableToString(MyTable, ", "))
-- Output: {[1] = "This table", [2] = "was converted into", [3] = 1, [4] = "string", [5] = "!"}
```
@param t {[any]: any} -- The table to convert to a string.
@param sep string? -- The separator between each key + value
@param i number? -- The index to start at. (only applies to arrays)
@param j number? -- The index to end at. (only applies to arrays)
@return string?
]=]
function Utility.TableToString(t: {[any]: any}, sep: string?, i: number?, j: number?): string?
if Utility.IsDictionary(t) then
Utility.dictionaryLen(t)
end
if #t == 0 then
return "{}"
end
local stringToConvert = "{"
sep = Utility.nilparam(sep, ", ")
i = Utility.nilparam(i, 1)
j = Utility.nilparam(j, #t)
if not Utility.IsDictionary(t) then
if i <= 0 or i > #t then
Debugger.error(`Field 'i' must be greater than 0 and less than or equal to {#t}.`)
return
end
if j <= 0 or j > #t then
Debugger.error(`Field 'j' must be greater than 0 and less than or equal to {#t}.`)
return
end
end
if Utility.IsDictionary(t) then
local current = 0
for key, value in t do
current += 1
if type(value) == "string" then
value = `"{value}"`
end
if type(value) == "table" then
value = Utility.TableToString(value, sep)
end
if current == #t then
stringToConvert = `{stringToConvert}["{key}"] = {value}}`
else
stringToConvert = `{stringToConvert}["{key}"] = {value}{sep}`
end
end
return stringToConvert
end
for index = i :: number, j :: number do
local value = t[index]
if type(value) == "string" then
value = `"{value}"`
end
if type(value) == "table" then
value = Utility.TableToString(value, sep)
end
if index == j then
stringToConvert = `{stringToConvert}[{index}] = {value}}`
else
stringToConvert = `{stringToConvert}[{index}] = {value}{sep}`
end
end
return stringToConvert
end