Heyo fellow developers! I’m here to share with the scripting enthusiasts out there a technology that I find rather underrated in the Roblox development community. Ever wondered about alternatives to Lua for your Roblox scripting needs ? This might interest you…
I do not apologize for the following wall of text since this is a whole scripting language we’re talking about and a reasonnably sized post would not do it justice, but if it’s too much for you there will be a TL;DR at the end.
What is it, and how does it work?
To summarize in a few words, Moonscript is a fully-fledged scripting language whose syntax is heavily inspired by CoffeeScript. I love it for its conciseness and readability, and also for how easy it is to define classes. But the most interesting aspect of Moonscript - and the reason why it is possible to use it with Roblox at all - is that this language compiles to Lua.
In other terms: you write your Moonscript code on your machine, then you run a program called the Moonscript compiler (which you can try online here or download here) whose job is to read a Moonscript program and convert it into an equivalent Lua program that would do the exact same thing when run.
But what’s the point of using that Moonscript thing if all it does is generate some Lua code? Why don’t you just directly write in Lua instead?
Here’s a bit of history for you. Back in 2011, there was this professional Lua developer who was so tired of dealing with Lua (who can blame him honestly) that he decided to make his own language that would be “like Lua, but better”. He then proceeded to create Moonscript and used it for developing websites such as itch.io. Anyways, his whole point was to create a language that would be more elegant, more readable than Lua while still being able to run it anywhere Lua can run (Like Roblox, for example). Moonscript might not be able to do anything more than Lua, but it does it better than Lua.
Ok, what’s the syntax like?
There’s already some great official articles presenting Moonscript and its syntax.
Moonscript’s homepage is a good place to start. If you’re convinced already and want the full list of Moonscript’s specificities, check out the reference manual.
I figured it would be good to have some example Moonscript code written for Roblox specifically. After all, Moonscript’s targets Lua in general, but thankfully Roblox’s Lua is exactly the same as good old Lua 5.1, apart from some global variables which can totally be used in Moonscript as well. Check out this demo game I made just for you. I commented it as best as I could, so it should not be too hard to dive into its source if you follow Moonscript’s reference manual alongside with it.
Looks cool! So how do I run some Moonscript on Roblox?
There are basically two ways to go…
-
The tedious way
This one involves a lot of copy and pasting, but it does not require you to install anything and as such it is great for trying out Moonscript.- Fire up the online Moonscript compiler
- On the left, write some moonscript. For example, try typing
print "Hello world!"
orp = Instance.new "Part", game.Workspace
(No parenthesis, because Moonscript is too cool for parenthesis) - Watch in awe as the equivalent Lua code appears on the right. Copy and paste that in a script in Roblox Studio, then run. Congratulation! You’ve just run some Moonscript in Roblox!
-
The real efficient way
Be aware that this one does require some setting up. You will be able to write Moonscript code on your machine (inside.moon
files) and sending those to Roblox in real time.- Get yourself a nice text editor. I recommend Atom, because it’s good, it’s freeeeee, and it’s the only one I found with decent Moonscript support (i.e. syntax coloration and linting plugins, don’t forget to install those too)
- Install the moonscript compiler from moonscript.org. Extract the zip somewhere and add it to your PATH
- Now all you need is a synchronization plugin, a program to send your
.moon
files from your machine to Roblox. I’d give you mine, however it is broken, inefficient and in French. I am currently in the process of revamping it, would you guys be interested in a link once it’s done? In the meantime, I suggest you use @evaera’s RbxSync (yeah it’s being deprecated but it still works well and I don’t think any other synchronization plugin out there supports Moonscript, though it’s in Rojo’s todo-list)
If you guys are interested I will definitely do a more detailed tutorial in a later post! This one is getting way too long already…
I’m still not convinced, what are some things Moonscript does better than Lua?
I’ll be talking from experience here. Trust me, I have eaten my fair share of Moonscript. In fact, I completely ditched Lua months ago. During the latest Accelerator Program, I scripted the entirety of Temple Thieves under three months without writting a single line of Lua.
What I found when working on that project is that Moonscript was extremelly helpful when it came to keep 12 weeks worth of code organized and not losing my mind with months old scripts, more than Lua ever was. Moreover, I was undeniably more efficient than I’ve ever been with Lua - @Frinigus and I managed to release Temple Thieves two whole weeks before the program’s end date.
Here are some specific aspects of Moonscript that make it so readable and efficient:
- Writing classes in Moonscript is the best. In Lua, you need to struggle with metatables to define classes. Good guy Moonscript manages metatables behind the scenes, allowing you to define a class in one line whereas it takes twenty times that to do so in Lua (This is not even an exageration, try typing
class MyClass
in the
Moonscript compiler and compare with the generated Lua…) - It introduces many shortcut statements. For example, check out Moonscript’s
with
,switch
orunless
statements in the reference manual - Lua has a bad tendency to quickly become bloated with keywords. Moonscript fixes that by omitting many of them, like
end
's (blocks are instead defined by their indentation, exactly like Python),do
's in loops,then
's in conditionnals, and much more. - Parenthesis are entirely optional!
- Assignment operators like
+=
,*=
, etc., which are supported in basically every languages EXCEPT LUA, are thankfully implemented in Moonscript. - In Moonscript, variable are local by default. No more of these
local
keywords everywhere. - The
self
keyword, which starts showing up everywhere when you abuse OOP, is reduced to one symbol,@
, further reducing bloating
You’re talking like Moonscript is the best thing that ever happened. Surely there are some downsides?
I might sound a little bit biased (It’s because I do think Moonscript is the best thing that ever happened), however I do admit that Moonscript is not for everyone. Take these into consideration:
- Moonscript is one whole scripting language, and you can’t learn that overnight. Though it will most likely benefit you in the long run, it will require a lot of getting used to. Until you’re familiar with it you will make the same stupid mistakes over and over again and that will impede your development for some time. For me it took a few weeks of working on small demo projects to get really comfortable. YMMV.
- It requires you to use a third party text editor. Same as above: you’ll need some time to adapt but since most text editors out there are way more powerful than Studio’s built-in editor, it will benefit you in time.
- Setting it up isn’t easy either. I’m considering working on some in-depths tutorials though, let me know if you would be interested!
- There might be some slight performance overhead. Moonscript does not necessarily generate the most efficient Lua code, however I can tell you from experience that it’s really negligible. I believe that you should never sacrifice readability for minor performance improvements but if you disagree with that Moonscript is probably not for you.
- If Moonscript code is nice and clean, the Lua code it generates is the exact opposite. Compiled code was never meant to be aesthetically pleasant (Open a random
.exe
file on your machine with Notepad. What did you expect? Neat lines of codes?). This makes it somewhat harder for debugging and working in teams impossible unless everyone uses Moonscript.