Skip to content
Introduction to Lua Scripting
Garry's Mod

Introduction to Lua Scripting

Learn Lua scripting for Garry's Mod. Understand variables, functions, control structures, and how to create custom content.

By ···10 min read·Multi-source verified
1 reading this guide  

Learn Lua scripting for Garry's Mod. Understand variables, functions, control structures, and how to create custom content.

13.1. Introduction to Lua Scripting

For those who wish to push the boundaries of Garry's Mod beyond pre-made addons, Lua scripting offers unparalleled power. This section provides an introduction to Lua, the scripting language used in GMod, and how it enables custom content creation.

Lua is a lightweight, powerful, and embeddable scripting language widely used in game development, including Garry's Mod. By learning Lua, you can create your own gamemodes, weapons, entities, user interfaces, and modify existing game mechanics. It's the backbone of much of the custom content available on the Steam Workshop.

Why Lua in Garry's Mod?

  • Flexibility: Lua allows for dynamic modification of game behavior.
  • Accessibility: It's relatively easy to learn compared to compiled languages like C++.
  • Community Support: A large community exists for GMod Lua scripting, offering resources and help.
  • Server-Side vs. Client-Side: Lua scripts can run on the server (affecting all players) or the client (affecting only You running the script), or both.

Basic Lua Concepts:

  • Variables: Used to store data (e.g., `local playerName = "Alice"`).
  • Data Types: Numbers, strings, booleans, tables (arrays/dictionaries), functions, etc.
  • Control Structures: `if/then/else` statements for conditional logic, `for` and `while` loops for repetition.
  • Functions: Blocks of code that perform specific tasks. Garry's Mod provides many built-in functions (e.g., `ents.Create()`, `player:Nick()`).
  • Tables: The primary data structure in Lua, used for lists and key-value pairs.

Getting Started with GMod Lua:

  1. Environment: Scripts are typically placed in the `garrysmod/lua/` directory. Server-side scripts go in `autorun/server/`, client-side in `autorun/client/`, and shared scripts in `autorun/`.
  2. Console Commands: You can execute simple Lua commands directly in the GMod console (enable with `~`).
  3. Learning Resources: The official Garry's Mod Wiki (wiki.facepunch.com) is an invaluable resource for API documentation and tutorials. Community forums and Discord servers are also excellent places to seek help.
  4. Example: A Simple Server-Side Script

    This script would print a message to the server console when the game starts:

    -- lua/autorun/server/my_first_script.lua
    
    print("Hello from my custom Lua script!")
    

Learning Lua scripting opens up a world of possibilities for customization and creation within Garry's Mod. Start with simple examples and gradually build your knowledge.

100% Human-Written. AI Fact-Checked. Community Verified. Learn how AntMag verifies content