Garry's Mod

Garry's Mod

Not enough ratings
How to create your first lua script.
By Dr Lisa Cuddy
In this guide you will be taught how to create basic lua scripts.
   
Award
Favorite
Favorited
Unfavorite
Introduction
The first task is to go ahead and download notepad++, this will be our code editor for this tutorial.

It can be downloaded Here[notepad-plus-plus.org]

Notepad++ is the program that we can use to create and edit lua scripts, it has syntax highlighting for the lua language.


Creating the script
The first script we will create will print to the console, to do this open notepad++ and paste this text:

print ("Hello, this is my first lua script")

Now, how do we run the script?

You must click "save as" in the notepad++ top left, and select file type as .lua lua script
Navigate to this directory C:\Program Files (x86)\Steam\steamapps\common\GarrysMod\garrysmod\lua and save the file as whatever you want to call it, in this example I will call it

example.lua

Now you have saved your first lua script.
Running the script
To run the script in game we have two choices, we can run it on singleplayer or at a server.

When you run a script you can either run it clientside which is on your own client, or serverside which is on your own server.

To run the script serverside you must be on a singleplayer game or it must be in the servers lua directory, you may type lua_openscript scriptnamehere.lua for example in the example script i would type
lua_openscript example.lua

If you want to run the script clientside, you can be either in singleplayer or on a server with the server console variable as sv_allowcslua 1 (Note you can also type this in your own singleplyaer game if the command is not working)
you will type
lua_openscript_cl example.lua
note the cl meaning Clientside
Learning lua.
To learn lua itself rather than just printing to console, you should visit the gmod tutorials page on the official gmod wiki which can be found Here[wiki.garrysmod.com] this also serves as a api reference for all the functions and hooks in the game.

Enjoy I hope you have learnt from this tutorial how to load lua scripts in to the game.

Thank you for reading.
4 Comments
Alexandrovich 13 Feb, 2024 @ 6:14am 
Need help with

hook.Add("Tick", "TEST", function()
Range = 15000
Position = player
BasePos = Vector(-2219.310791, -3163.419678, -307.161743)
end)
if BasePos:Distance( Position ) < Range then
timer.Simple(5, function()
chat.AddText( Color(40,255,40,255), "YOU ARE NEAR")
end)
end

Please...
huu 21 Sep, 2019 @ 2:14pm 
can i see your github for your "anti cheat"
Dr Lisa Cuddy  [author] 25 Aug, 2019 @ 1:14pm 
Why are you posting troll comments?
E 25 Aug, 2019 @ 12:55pm 
shit guide lmao