repr — function for printing Lua tables

Hey all!

I wrote a function that will come in handy when printing tables in Lua. Normally, when you call print on a table, it is first tostring-ed into a memory address and looks something like table: 000002074CD2C070. How unhelpful! If only there was a better way…

I’ve created a function, repr, that works like Python’s repr. It returns a nice, printable representation of Lua values (strings, numbers, bools, and of course tables). It is designed with two goals in mind: (1) be as close as possible to a literal code representation and (2) be as useful as possible during debugging. Check it out at this GitHub repository, or keep reading this post!

Pretty-print your tables with newlines and indendation by providing a second settings table.

Click here to see the pretty-print example output

Uses

This function is perfect for learning the structure of complicated tables returned by Roblox functions, such as the recently-released AssetService:GetBundleDetailsAsync. Try this in the command bar:

Result of AssetService:GetBundleDetailsAsync(492), converted to a string by repr

Protip! Install the plugin (see below), and you’ll be able to access _G.repr in the Command bar!

Features

  • Can pretty-print with newlines and indentation (tabs or spaces)
  • Works recursively for sub-tables
  • Alphabetizes keys automatically
  • Keys are properly quoted if they aren’t valid identifiers
  • Can print the full name and (optionally) the class of Roblox objects
Click here for an in-depth example that shows off all the bells and whistles!

GitHub Repository

Maybe you scrolled past the link the second paragraph, but just in case: repr is available on GitHub. You can download the source code directly here: repr.lua.

Free Model

It’s a ModuleScript named MainModule. You can load it like this:

Plugin

This plugin automatically loads the above free model and stores the function in _G.repr, very useful when debugging using the Command bar. This is the source code:

This enables you to do useful stuff like this within Command bar in Studio:

Source Code

Disclaimer: the Free Model may be more up-to-date than this post.

Click here to show the source code for repr v1.1

Version History

  • 5 May 2019 – v1.0 Initial release
  • 6 May 2019 – v1.1 Now supports all Roblox data types (Vector3, Color3, etc.)

Portability

This function won’t break in non-Roblox Lua environments, like LÖVE, Gideros, Gary’s Mod etc. Hello to our fellow Lua programmers from the great beyond 🙂

Licensing

I license this work (the Lua code, model, and plugin) under the WTFPL. Go nuts. Let me know if it helped you make something cool. Please don’t to use it for world domination (it’s where I keep my stuff).

Devforum Thread

I’ve posted this to Roblox’s Developer Forum. You can leave your questions, comments and concerns over there, or send them to me on Twitter.

Author: Ozzypig

Roblox developer and computer scientist. I love all things coding and game design!