Coding - Getting Started
A lua file is called a script, it's plain text. To create and edit these scripts you need a plain text editor such as notepad.
You can use any text editor, but to make your life easier, we recommend that you use Notepad++. A plugin you may want to use with Notepad++ would definitely be the Gmod Lua highlighter: https://github.com/kylefleming/npp-gmod-lua
For the sake of simplicity we'll refer to both 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 send a message to the console.
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 create the actual Lua script file. To find your lua folder follow the following path - this may be slightly different on your computer, 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.
To run any of your scripts you need to be playing a map. Now, open the console and type the following :
Press enter. If you did everything correctly up to this point you should see a message in the console :
To make the script run automatically when the server starts, you simply put it into one of those folders:
- For shared scripts ( Loaded on both client and 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 then do the following as specified
- To load on the client
- To load on the server
- To load onto shared do neither
Next we will learn about variables.
Clientside and Serverside Explained Here: States