How do I read tables during in-game testing?

Hello, I’m currently making a game where essential data is transferred through TeleportData. Therefore, I cannot use the Roblox Studio client when testing anything that isn’t the starter place. However, if I want to see the contents of a table to ensure that my code is working, I can’t. How can I fix this?

If this teleport data doesn’t have layers of tables, you can loop through it and print keys and values. Otherwise I’d recommend adding an inspect module to the game. Any version you find online will probably work.

for i, v in teleportData do
    print(i, v)
end

It can be this simple assuming there are no tables nested in the teleport data.

If there are nested tables, you would need a recursive approach, preferably with indentation.

Recursive Approach
-- Google Gemini, avocado (@avodey) -w-

local function formatTable(data: {}, _indent: number?, _trace: {}?): string
	_trace = _trace or {}
	_indent = _indent or 1
	
	if _trace[data] then
		return "***CYCLIC REFERENCE***"
	end

	_trace[data] = true
	
	local indentStr = string.rep("  ", _indent)
	local nextIndentStr = string.rep("  ", _indent + 1)
	
	local isArray: boolean do
		isArray = true
		local nextIndex = 1

		for k, v in data do
			if type(k) ~= "number" or k ~= nextIndex then
				isArray = false
				break
			end

			nextIndex += 1
		end
	end
	
	--\\ Write Pairs
	
	local lines: {string} = {}
	
	for k, v in data do
		local displayKey: string
		
		if isArray then
			displayKey = ""
		elseif type(k) == "string" and string.match(k, "^[a-zA-Z_][a-zA-Z0-9_]*$") then
			displayKey = k .. " = "
		elseif type(k) == "number" then
			displayKey = "[" .. tostring(k) .. "] = "
		else
			displayKey = "[\"" .. tostring(k) .. "\"] = "
		end

		local displayValue: string

		if type(v) == "table" then
			local newVisited = {}
			
			for t in _trace do
				newVisited[t] = true
			end

			displayValue = formatTable(v, _indent + 1, newVisited)
		elseif type(v) == "string" then
			displayValue = string.format("\"%s\"", v)
		else
			displayValue = tostring(v)
		end

		if isArray then
			lines[#lines + 1] = nextIndentStr .. displayValue .. ","
		else
			lines[#lines + 1] = nextIndentStr .. displayKey .. displayValue .. ","
		end
	end
	
	--\\ Concat Results
	
	if #lines > 0 then
		local lastLine = lines[#lines]
		lines[#lines] = string.sub(lastLine, 1, #lastLine - 1)
	end

	return "{\n" .. table.concat(lines, "\n") .. "\n" .. indentStr .. "}"
end

--\\ Test

local data = {}
data[1] = data
data[2] = {
	Hello = true,
	Nested = {
		1, 2, 3, {4, 5}, 6, print
	},
}

print("\n" .. formatTable(data))

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