Garry's Mod
Garry's Mod

Lua Scripting for GMod (Beginner)

Learn beginner Lua scripting for Garry's Mod. Understand variables, functions, control structures, and basic GMod interactions to start creating addons.

Lua Scripting for GMod (Beginner)

Lua scripting is the backbone of Garry's Mod addon development, allowing you to create custom mechanics, gamemodes, and interactive elements. This beginner's guide introduces the fundamental concepts of Lua programming within the Garry's Mod environment, covering variables, functions, basic control structures, and how to interact with the game world.

Embark on your Garry's Mod scripting journey with this beginner's guide to Lua. Learn the essential building blocks of programming: variables, data types, functions, and control flow. We'll cover how to write simple scripts that interact with the GMod environment, setting you on the path to creating your own addons and gamemodes.

What is Lua?

Lua is a lightweight, powerful, and easy-to-learn scripting language. Garry's Mod uses Lua extensively for its addon system, gamemodes, and server-side logic.

Basic Lua Concepts
  • Variables: Used to store data. In Lua, variables are dynamically typed. Example: local myVariable = 10
  • Data Types: Common types include numbers (integers and floats), strings, booleans (true/false), tables (arrays and dictionaries), and nil.
  • Functions: Blocks of code that perform a specific task. They can take arguments and return values. Example: function MyFunction(arg1, arg2) ... end
  • Comments: Use -- for single-line comments or --[[ ... ]] for multi-line comments.
Control Structures
  • `if` statements: Execute code based on conditions. Example: if myVariable > 5 then print('Greater than 5') end
  • `for` loops: Iterate a specific number of times. Example: for i = 1, 10 do print(i) end
  • `while` loops: Execute code as long as a condition is true. Example: while someCondition do ... end
Interacting with Garry's Mod
  • Printing to Console: Use print('Hello, GMod!') to display messages in the game console.
  • Spawning Entities: You can spawn entities using Lua. Example: ents.Create('prop_physics').
  • Hooks: Garry's Mod uses hooks to trigger functions at specific game events (e.g., player joining, entity being created). Example: hook.Add('PlayerSpawn', 'MyPlayerSpawnHandler', function(ply) print(ply:Nick() .. ' has spawned!') end)
Your First Script

To run a simple script:

  1. Navigate to your Garry's Mod addons folder (usually `Steam/steamapps/common/GarrysMod/garrysmod/addons/`).
  2. Create a new folder for your addon (e.g., `my_first_addon`).
  3. Inside that folder, create a `lua` folder, and inside that, a `autorun` folder.
  4. Create a new text file named `init.lua` inside the `autorun` folder.
  5. Paste the following code into `init.lua`:
print('My first GMod script is running!')

local function greetPlayer(ply)
 print('Welcome, ' .. ply:Nick() .. '!')
end

hook.Add('PlayerInitialSpawn', 'WelcomeMessage', greetPlayer)

Start Garry's Mod, and you should see 'My first GMod script is running!' in the console. When you spawn, you'll see a welcome message for your character.

This is just the beginning. As you become more comfortable, explore the Garry's Mod Wiki for a comprehensive list of functions and hooks.