Revision Difference
Beginner_Tutorial_Intro#528523
<cat>Dev.GettingStarted</cat>
<title>Coding - Getting Started</title>
# Introduction
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".
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 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 notepad and Notepad++ as "notepad".
<warning>
**Do not use Wordpad.**
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.
</warning>
# Creating the script
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 :
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 <page>Global.print</page> function does exactly that. Type the following code into your chosen editor :
```
print( "Hello world" )
```
You're done! Wasn't that easy? It should have been.
## Explanation
`print` is a function. A function is a command that does something when you call it. Many functions can take arguments, which is data you give the function to change exactly what it does. In this case, `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.
# Saving the script
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:
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:
```
C:\Program Files\Steam\steamapps\common\garrysmod\garrysmod\lua\⤶
C:\Program Files\Steam\steamapps\common\GarrysMod\garrysmod\lua\⤶
^^^^^^^^^^^^^^^^^^^^^^⤶
This may be different!⤶
```
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.
⤶
<warning>⤶
**Check the "File name extensions" box in File Explorer**⤶
⤶
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).⤶
</warning>⤶
# Running the script
To run any of your scripts you need to be playing a map. Now, open the console and type the following :⤶
To run any of your scripts, you need to be playing a map. Once in game, open the developer console and type the following:⤶
```
lua_openscript helloworld.lua
```
Press enter. If you did everything correctly up to this point you should see a message in the console :⤶
Press enter. If you did everything correctly up to this point, you should see a message in the console:⤶
```
Hello World!
```
Congratulations, you have written and run your first Lua script.⤶
⤶
⤶
# Auto running the script
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 )
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 )
```
C:\Program Files\Steam\steamapps\common\garrysmod\garrysmod\lua\autorun⤶
C:\Program Files\Steam\steamapps\common\GarrysMod\garrysmod\lua\autorun⤶
```
* For client-only scripts ( Loaded only on client )
```
C:\Program Files\Steam\steamapps\common\garrysmod\garrysmod\lua\autorun\client⤶
C:\Program Files\Steam\steamapps\common\GarrysMod\garrysmod\lua\autorun\client⤶
```
* For server-only scripts ( Loaded only on server )
```
C:\Program Files\Steam\steamapps\common\garrysmod\garrysmod\lua\autorun\server⤶
C:\Program Files\Steam\steamapps\common\GarrysMod\garrysmod\lua\autorun\server⤶
```
For this specific tutorial any of those paths will work just fine.
For this specific tutorial, any of those paths will work just fine.
<note>All client and shared files must be put for download using <page>Global.AddCSLuaFile</page> for them to work in multiplayer.</note>
# Loading portions of a script on the client or server
If you want to use a single file for clientside and serverside code then do the following as specified⤶
* To load on the client
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
```lua
if CLIENT then
--Your clientside script here
--Your clientside code here
end
```
* To load on the server
* To run code only on the server
```lua
if SERVER then
--Your serverside script here
--Your serverside code here
end⤶
```⤶
⤶
* To load onto shared (either Client **or** Server), don't specifically check for `CLIENT` or `SERVER`, just put your code down without the `if` block.⤶
⤶
Example:⤶
⤶
```lua⤶
print( "Hello Shared!" )⤶
⤶
if SERVER then⤶
print( "Hello Server!" )⤶
end⤶
⤶
if CLIENT then⤶
print( "Hello Client!" )⤶
end
```
⤶
* To load onto shared do neither⤶
The `CLIENT` and `SERVER` variables you see up there are explained on the <page>States</page> page.⤶
# What's next?
Next we will learn about <page text="variables">Beginner_Tutorial_Variables</page>.
Clientside and Serverside Explained Here: <page>States</page>
Clientside, Serverside, and Shared are explained here: <page>States</page>