Format table as string

Hello, I am making a modified version of error() to check for errors more easily, also knowing what variables are there, example:
image


The problem, when printing tables, tables appear as in memory address in the game

So, I wanted to ask for help to format a table to string as seen in studio
image

I tried, but I can’t format it correctly, function

local function debug(Base: string, ...)
	local NewObjs = {}
	for _, v in pairs({...}) do
		if typeof(v) == "Instance" then
			table.insert(NewObjs, ("%s: %s"):format("Instance - "..v.ClassName, tostring(v)))
			
		elseif typeof(v) == "Color3" then
			table.insert(NewObjs, "Color: "..v:ToHex()
			
		elseif typeof(v) == "table" then
			-- ???
			
		else
			table.insert(NewObjs, ("%s: %s"):format(typeof(v), tostring(v)))
			
		end
	end
	error(Base.." \n	"..table.concat(NewObjs, "\n	"))
end

I wrote something to do this awhile ago, it dumps a table’s metatable as well. Bear with me, it’s messy:

local function dump(table, level)
	level = level or 1
	local buffer = string.rep(' ', level * 4)
	local bufferMinus1 = string.rep(' ', (level - 1) * 4)
	local currentString = '{'
	local didInsert = false
	for i,v in pairs(table) do
		didInsert = true
		currentString = currentString .. '\n' .. buffer .. '[' .. (type(i) == 'string' and ('"' .. i .. '"') or tostring(i)) .. '] = ' .. (type(v) == 'string' and ('"' .. v ..'"') or type(v) == 'table' and (dump(v, level + 1)) or (tostring(v)) .. ';')
	end
	currentString = currentString .. (didInsert and ('\n' .. bufferMinus1) or '') .. '};'
	return currentString
end

local function longestLine(tableOfStrings)
	local longest, index = -1, 0
	for i,v in ipairs(tableOfStrings) do
		local len = v:len()
		if len > longest then
			longest, index = v:len(), i
		end
	end
	return longest, index
end

local headerTable, headerMetatable = 'Table:', 'Metatable:'
local metatableSpaceBuffer = 4;

local function tableToString(t, doMetatable)
	local str = dump(t)
	local metatable = getmetatable(t)
	if not metatable or not doMetatable then
		return str
	end
	local split = string.split(str, '\n')
	local longestLength = longestLine(split)
	local metaStr = dump(metatable)
	local metaDumpSplit = string.split(metaStr, '\n')
	local header = headerTable .. string.rep(' ', longestLength + metatableSpaceBuffer - headerTable:len()) .. headerMetatable .. '\n'
	for i,v in ipairs(split) do
		if metaDumpSplit[i] then
			split[i] = split[i] .. string.rep(' ', longestLength + metatableSpaceBuffer - v:len()) .. metaDumpSplit[i]
		end
	end
	if #metaDumpSplit > #split then
		for i = #split + 1, #metaDumpSplit do
			table.insert(split, string.rep(' ', longestLength + metatableSpaceBuffer) .. metaDumpSplit[i])
		end
	end
	return header .. table.concat(split, '\n')
end 

To use it, just call tableToString with a table, and a boolean to specify whether or not to dump the metatable as well.

image

Can’t guarantee it’ll work well with the dev console/non-monospaced fonts tho.

Edit: fix missing variables for metatable dump

1 Like

Works perfectly, thank you very much!

1 Like

yo is there a way to do string to table ?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.