Coding - Getting Started
A Lua file is called a script. Lua scripts are plain text files. To create and edit Lua scripts, you need a plain text editor such as notepad.
You can use any plain text editor, but you are going to have a hard time if you aren't using one of the many available Lua Editors, such as Sublime Text or Visual Studio Code.
During these tutorials, we'll refer to your preferred text editor as "notepad".
It is a formatted text editor, not a plain text editor. It will put useless tags in around your code that will cause it to fail when loading. As a rule of thumb, if you can have text that uses two different fonts in one file, then it is not a plain text editor.
For our first script, we're not going to do anything too complex. We'll learn how to print a message into the console.
The print function does exactly that. Type the following code into your chosen editor :
You're done! Wasn't that easy? It should have been.
print() takes only one argument, which is a string (a series of letters, numbers, spaces, and so on), and when
print() is called, it puts that string into the console in Garry's Mod.
You're now ready to save the actual Lua file. To find your Lua folder, follow the following path - depending on where you have installed Garry's Mod on your computer, this path may be different, but will look something like this:
In the filename box, type
helloworld.lua (Note that you must specify
.lua), and in the Save As type box, select All Files. Now, just press enter (or press the save button) to save your script.
Make sure you have the "File name extensions" checkbox checked in File Explorer. Unless this is checked, your text editor might accidentally save your
.lua file as a
.lua.txt. To make sure this box is checked, open a File Explorer window, click the
View tab at the top, and check the box (it's inside the "Show/hide" category at the top).
To run any of your scripts, you need to be playing a map. Once in game, open the developer console and type the following:
Press enter. If you did everything correctly up to this point, you should see a message in the console:
Congratulations, you have written and run your first Lua script.
To make your script run automatically when the server starts, you simply put it into one of these folders. Again, if you have Garry's Mod installed somewhere else, navigate there instead.
- For shared scripts ( Loaded on either client or server )
- For client-only scripts ( Loaded only on client )
- For server-only scripts ( Loaded only on server )
For this specific tutorial, any of those paths will work just fine.
If you want to use a single file for clientside and serverside code, but want separate parts of the code between client and server, we can do something like this:
- To run code only on the client
- To run code only on the server
- To load onto shared (either Client or Server), don't specifically check for
SERVER, just put your code down without the
SERVER variables you see up there are explained on the States page.
Next we will learn about variables.
Clientside, Serverside, and Shared are explained here: States