Skip to content
Basic Lua Syntax and Variables
Garry's Mod

Basic Lua Syntax and Variables

Learn Garry's Mod Lua basics: syntax, comments, variables (global/local), data types (string, number, table), and control flow (if, for).

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

Learn Garry's Mod Lua basics: syntax, comments, variables (global/local), data types (string, number, table), and control flow (if, for).

Unlock the power of Lua scripting in Garry's Mod by understanding its fundamental syntax and variables. This section introduces the basics of Lua, the scripting language that drives much of the game's functionality, enabling you to create custom entities, gamemodes, and more.

Introduction to Lua

Lua is a lightweight, powerful scripting language widely used in game development, and Garry's Mod leverages it extensively. Whether you're creating a simple prop that reacts to damage or developing an entirely new gamemode, a grasp of Lua is essential. This guide covers the foundational elements of Lua syntax and how variables work.

Basic Syntax Elements

  • Comments: Lines starting with `--` are comments and are ored by the interpreter. They are used for explanations. Multi-line comments are enclosed in `--[[ ... ]]--`.
  • Statements: Lua statements are typically separated by newlines. Semicolons (`;`) can be used but are often optional.
  • Keywords: Lua has reserved keywords like `if`, `then`, `else`, `end`, `while`, `do`, `for`, `function`, `local`, `return`, etc.
  • Operators: Lua supports arithmetic operators (`+`, `-`, `*`, `/`, `%`), comparison operators (`==`, `~=`, `<`, `>`, `<=`, `>=`), and logical operators (`and`, `or`, `not`).

Variables: Storing Information

Variables are named containers used to store data. In Lua, variables are dynamically typed, meaning you don't need to declare their type explicitly.

Global vs. Local Variables:
  • Global Variables: If you declare a variable without the `local` keyword, it becomes a global variable, accessible from anywhere in your script. Overusing global variables can lead to naming conflicts and make your code harder to manage.
  • Local Variables: Declaring a variable with the `local` keyword makes it local to the current scope (e.g., a function or a code block). This is the preferred method for most variables as it improves performance and prevents naming conflicts.
Variable Assment:

You ass a value to a variable using the equals s (`=`).

-- Global variable
myGlobalVariable = 10

-- Local variable
local myLocalVariable = "Hello, Garry's Mod!"

Data Types in Lua

Lua supports several basic data types:

  • Nil: Represents the absence of a value.
  • Boolean: `true` or `false`.
  • Number: Represents floating-point and integer numbers.
  • String: Sequences of characters, enclosed in single (`'`) or double (`"`) quotes.
  • Table: Lua's only data structuring mechanism. Tables are associative arrays, meaning they can be indexed by numbers, strings, or any other value (except `nil`).
  • Function: Blocks of code that can be executed.
  • Userdata: Represents C data structures.
  • Thread: Used for coroutines.
Example: Using Different Data Types
local playerName = "Garry"
local playerHealth = 100
local isAlive = true
local inventory = {"Pistol", "Medkit"} -- A table (array)

print(playerName) -- Outputs: Garry
print(playerHealth) -- Outputs: 100
print(isAlive) -- Outputs: true
print(inventory[1]) -- Outputs: Pistol (accessing the first element of the table)

Basic Control Flow: `if` Statements

Conditional statements allow your code to make decisions.

local score = 50

if score > 75 then
 print("Excellent score!")
elseif score > 50 then
 print("Good score.")
else
 print("Keep practicing.")
end

Basic Control Flow: `for` Loops

Loops are used to repeat code.

-- Numeric for loop
for i = 1, 5 do
 print("Iteration: " .. i)
end

-- Looping through a table
local colors = {"Red", "Green", "Blue"}
for index, value in ipairs(colors) do
 print(index .. ": " .. value)
end

Understanding these fundamental Lua concepts – variables, data types, and basic control flow – is the crucial first step in harnessing the scripting capabilities of Garry's Mod. As you progress, you'll build upon these foundations to create increasingly complex and interactive elements.

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