Moonscript, an alternative to Lua for Roblox scripting - Why (and why not) you should use it

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.
    1. Fire up the online Moonscript compiler
    2. On the left, write some moonscript. For example, try typing    print "Hello world!"   or   p = Instance.new "Part", game.Workspace   (No parenthesis, because Moonscript is too cool for parenthesis)
    3. 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.
    1. 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)
    2. Install the moonscript compiler from moonscript.org. Extract the zip somewhere and add it to your PATH
    3. 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 or unless 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.

TL;DR

57 Likes

Although I do agree Moonscript does a great job at cleaning up Lua, one of the biggest complaints I had when using it was how it compiled the end result. Some of it’s methods are … quite inefficient to say the least. Additionally, it has some querkyness when writing code in certain ways where the compiler gets confused and compiles syntax errors. Nonetheless, it does a fantastic job cleaning up as it was intended to.

13 Likes

Idk about that one. I just avoid it and my life is pretty easy.

16 Likes

That’s one way to put it. It’s all about performance versus readability. I do think it’s worth the small performance overhead but I can see why it would turn down some people

2 Likes

It’s true that you can get a lot done without using metatables, however they’re so powerful. But also really cumbersome to use, I’m really conflicted about these. To be honest classes is the #1 reason I use Moonscript.

1 Like

It’s useful and not useful at the same time.

For people who don’t want to read a lot of Text just check this out

Language Guide - MoonScript 0.5.0

It’s a lot better to see code samples

No offense @Asleum , Moonscript is pretty cool :+1:, just not for me.

4 Likes

I’m looking through the reference right now: Language Guide - MoonScript 0.5.0
Can you convince me why all these white space hacks are actually worth it? (Particularly
the difference in how this compiles

a = x - 10
b = x-10
c = x -y
d = x- z

to

local a = x - 10
local b = x - 10
local c = x(-y)
local d = x - z

)

I wouldn’t call it a struggle. It is pretty simple and easy for me to work with. On top of that you can just make a module to handle a lot of the backend for you. Write once, apply anywhere.

When it comes to Lua-like languages i’d much prefer Wren if there was a Wren<->Lua transcompiler.

1 Like

PLEASE don’t use MoonScript if you’re going to be working with others.

@evaera had a sudden urge to use MoonScript and by god the Lua code it turned into was literal garbage.

This isn’t really an excuse tbh, evaera and @Osyris made this and it supposedly is completely readable after being turned to Lua.

6 Likes

As someone who has no idea to do the most basic things for scripting such as “Hello world”, that would like to get into scripting, would you suggest learning Moonscript or Lua first?

Lua.

5 Likes

Was leaning towards that. Thanks!

Not much of an alternative, seems like more work to get anything done.

3 Likes

I don’t recommend using MoonScript, but I am pretty sure that you can already use MoonScript with Rojo today. MoonScript can compile to Lua in-place, so the .lua files will be right next to the .moon files, and then you could just sync those in with Rojo normally. You can even set the MoonScript compilation as a build step in your IDE, and then hide .lua files in your IDE file explorer, so it would all happen transparently in the background.

With a setup like that, you wouldn’t need any special tool or support from Rojo to work with MoonScript if you really want to do so.

1 Like

Moonscript is ok but not being able to read code is fairly objective and I can read 400+ lines of engine simulation code and understand what the math is calculating and what is happening where. I can also very easily optimize my code. The problem with Moonscript is that it gets compiled to Lua anyways so if you lose the source you end up with code that is different from what you originally wrote. It is also harder to debug considering traceback would refer to lua lines and code. There is also a disconnect between learning roblox API on the wiki and implementing it as the examples would need to be adjusted and re formatted. Metatables do not need to be used typically and if you are using them you really do need a class or youre just trying to shove as much OOP bloat as possible into the code because thats your coding style. Honestly (imo) I don’t believe this is very useful, at least not for people who are starting to learn coding. Your argument is influenced by personal bias so it doesnt really help us understand what the actual problems with lua are apart from a few quirks from itself.

5 Likes

MoonScript is pretty readable if you don’t use classes. If you do, you should learn to read the outputted garbage because you may need to debug it. Also, MoonScript tries to throw in returns and the end of your code, and if you don’t watch it you may end up with a logic error that will prevent your code from continuing in Roblox.
It does make life easier until you try to use classes though.

1 Like

When coding within moonscript does it support new Roblox endpoints i.e. DataStores, Pathfinding, and etc?
I am somewhat interested in coding in moonscript.

Also defining classes can be done simply easier using a premade module script if you’re calling it over and over again.
Lua 5.1 was pushed out in 2012 but metatables allowed developers to modify Lua endpoints since then.
I still don’t see the major point of using moonscript here. Lua is easy for me to read as long you have Notes put down on what part does what there shouldn’t be any difficulty reading lua.

Another downside is to try to get the major part of the Lua community to learn to code in MoonScript. As most of the programming community probably haven’t heard of MoonScript or know how to code using MoonScript but again we can just get the Lua only part of the community to just read the lua part of moonscript

I think I’ll stick to being a “Virgin Lua Coder”. As it is, I’m already trying to expand my Lua knowledge and suddenly cutting in on that diligence by using another coding language, which seems like nothing more than a compiler and alternative way to write Lua code, doesn’t sit well with me.

Moonscript looks like something interesting to check out, but in the end I think that its not entirely worth it. You’re much better learning off raw Lua and being productive over sorting out the issues that Moonscript gives you.

Nice meme tl;dr though.

3 Likes

Defining classes is easy enough, but full-on OOP (Inheritance, initialization, etc) is a little more work when you are using Lua. Moonscript handles your end of OOP pretty well.
As far as endpoints go, it can do everything Lua can do, both limitations and abilities. Think of Moonscript as another way of coding in Lua.
Having other people use Moonscript doesn’t matter too much, this isn’t a new standard or anything. If you are wanting other people to be able to read your code, you probably shouldn’t be using Moonscript.

I’ll probably switch between MoonScript and Lua for programming. Just like how you said I feel like MoonScript should be an alternative but do you recommend coding using Lua and MoonScript in the same game?