Lua Overview

From GMod Wiki

Jump to: navigation, search

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.



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") 
 



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:

  1. Replace "YourGamemodeName" with the name you want for your gamemode.
  2. 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\
  3. 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.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox