HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to LuaVariablesNumbers & MathStringsTablesConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Table
🔁 Condition & Loop
🧩 Function
Lua›Variables

Variables

Local variables with local, global variables, and nil.

Simple enough for a 5-year-old to understand

local and nil

Example code
1
local name = "Alex"
Local variable.
VariableLocal variable.
2
local x
nil if no value is assigned.
Variablenil if no value is assigned.
3
print(name)
Alex
Output / InputAlex
4
print(x == nil)
true
Output / Inputtrue
VariableOutput / Input
💡 Hint: Forgetting local makes a variable global; always use local when possible.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Define and print a local city = "Rize".

🎯 Expected output: Rize

Loading editor…

About this topic

In Lua, you don't write types when defining variables; the value you assign determines the type. It is completely fine to assign a number first and then a text to the same variable — the language allows this and gives no warning.

The most critical detail in Lua is this: if you don't write local, the variable becomes global. This goes unnoticed in small scripts, but leads to name collisions and very hard-to-find bugs in large projects. Experienced Lua programmers define almost every variable as local.

Reading an undefined variable does not throw an error, it returns nil. This flexibility silences typos: when you misspell a variable name, the program doesn't crash, it just behaves unexpectedly.

What you will learn

  • Variable definition
  • The difference between local and global scope
  • Undefined variables being nil
  • Multiple assignment
  • Naming conventions

Who is this for?

  • Beginners to Lua
  • Those learning the concept of scope
  • Those experiencing name collisions while writing game plugins

When to use?

Every value that shouldn't leak outside the script must be local; this is both secure and runs faster because it doesn't require accessing the global table.

Global variables should only be used for settings that truly need to be shared across all scripts.

Common mistakes

Wrong

function hesapla()
  sonuc = 42
end
hesapla()
print(sonuc)

Since local was not written, the result became a global variable and continues to live even after the function ends. The program works, but the function has leaked outside; another script using the same name can overwrite this value. In game plugins, such leaks are a classic source of bugs.

Correct

function hesapla()
  local sonuc = 42
  return sonuc
end
print(hesapla())

Wrong

local toplam = 10
print(Toplam)

Lua is case-sensitive; Total is an undefined name. You won't get an error, nil is printed on the screen. The fact that a typo silently produces nil is the most prominent behavior that makes debugging difficult in Lua.

Correct

local toplam = 10
print(toplam)

Wrong

local a, b = 1
print(a + b)

Since the right side is incomplete in the multiple assignment, the b value becomes nil and the addition throws the "attempt to perform arithmetic on a nil value" error. Lua fills missing values with nil without warning; therefore, one must make sure the counts match in multiple assignment.

Correct

local a, b = 1, 2
print(a + b)

Frequently asked questions

Why is it recommended to make every variable local?

For three reasons: it doesn't pollute the global namespace, there is no risk of collision with other scripts, and local access is measurably faster because global variables are looked up in a table. In Lua, this is not a style preference, but a practical necessity.

How do I delete a variable?

By assigning nil to its value. In Lua, nil means "no value"; when you assign nil to a variable, the garbage collector can free that value. Deleting elements from tables is also done by assigning nil in the same way.

How do I find out the type of a variable?

The type function returns the name of the type as text: number, string, boolean, table, function, nil, or userdata. It is the fastest way to check if a value is of the expected type when debugging.

Summary

If local is not written, the variable becomes global.

Undefined variables do not throw an error, they return nil.

The language is case-sensitive.

In multiple assignment, missing values become nil.

The type function returns the type.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code