Lua Overview
From GMod Wiki
This is just a quick overview of lua. For more info on the language, click here. If you wish to know more about scripting with lua in Garry's Mod, view some of the tutorials.
- Lua is the scripting language used in Garry's Mod. Lua is a simple and easy to learn language.
- Lua scripts can be written and changed in any text editor.
- Lua Files or Scripts require no compiling! Meaning that you will only have to write them, and Garry's Mod will read from them.
- What does Lua look like? Some examples:
This says "Hello World!" to the console.
Msg("Hello World!\n");
Some basic maths:
number=1 number=number*2 //number is now 2 (1x2) number=number+5 //number is now 7 (2+5) number=number-1 //number is now 6 (7-1) number=number/3 //number is now 2 (6÷3)
This creates the function SayMyName, and uses it to send "Hey Fred!" to the console:
function SayMyName(name) //Creates the function, which has the variable "name" Msg("Hey "..name.."!\n") //This sends the message with the variable e.g. if "name" is "George", then it replaces "name" with "George" end //end of the function sayMyName("Fred")
- Lua can be used to make a large range of things in Garry's Mod such as weapons, tools and even vehicles!
- Lua files/scripts need to be added to the correct folders for them to work.
On PC:
For weapons:
C:\Program Files\Steam\steamapps\yourusername\garrysmod\garrysmod\lua\Weapons
For gamemodes:
C:\Program Files\Steam\steamapps\yourusername\garrysmod\garrysmod\gamemodes\YourGamemodeName\gamemode\
On Mac:
For weapons:
~/Library/Application Support/Steam/SteamApps/{steam_user}/garrysmod/garrysmod/lua/weapons
For gamemodes:
~/Library/Application Support/Steam/SteamApps/{steam_user}/garrysmod/garrysmod/gamemodes/{YourGameModeName}/gamemode/
NOTE:
- Replace "YourGamemodeName" with the name you want for your gamemode.
- The gamemode's lua files MUST go inside a folder named "gamemode" in the folder for your gamemode, or it will not work. E.g. If I created a build gamemode named "Build", I would put all the files in this folder:\garrysmod\garrysmod\gamemodes\Build\gamemode\
- If you are running a 64 bit version of Windows, use:
Program Files (x86)
Instead of:
Program Files
Where to go now
This page is only of basic lua information.
Before you start learning lua, is is recommended you read this page so that you have everything you need to start creating lua scripts.
If you already have a script editing program, then it is time to start reading the tutorials.