Garry's Mod

Garry's Mod

Not enough ratings
Making and Running Scripts
By PURE FEAR
How to create and run script!!!
   
Award
Favorite
Favorited
Unfavorite
Making and Running Scripts
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".

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.

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 :

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 Msg() 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:

C:\Program Files\Steam\steamapps\common\garrysmod\garrysmod\lua\
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.

Running the script

To run any of your scripts you need to be playing a map. Now, open the 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 :

Hello World!

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 )
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
For server-only scripts ( Loaded only on server )
C:\Program Files\Steam\steamapps\common\garrysmod\garrysmod\lua\autorun\server
For this specific tutorial any of those paths will work just fine.

Note, that all client and shared files must be put for download using AddCSLuaFile for them to work in multiplayer.

Hope you enjoyed my guide. Have a great day guys!
7 Comments
Game Libary 23 Mar, 2021 @ 5:52am 
Copy and pasted from the gmod side xD... https://wiki.facepunch.com/gmod/Beginner_Tutorial_Intro
Chloro 26 Jan, 2021 @ 7:31am 
very cool :spycon:
Air Conditioner 3 Dec, 2020 @ 2:32pm 
kosher, if you are not already try putting (") on it EX: bind (key) "lua_openscript test.lua", copy and paste that and replace the (key) with the key you want, commands can only have spaces if they have the (") character before and after the command.
Kosher King of Israel 3 Feb, 2019 @ 10:02pm 
For some reason it won't let me bind lua_openscript to anything.
Jimboboiii 13 Apr, 2018 @ 7:08pm 
that was hard
dow 15 Aug, 2017 @ 12:06pm 
HOLY SHIT IM GANNA BUST A FAT ONE
PURE FEAR  [author] 12 Aug, 2017 @ 9:39pm 
Make sure to rate and favorite!! Thanks have a nice day!!!!