Skip to content
Skills Library
Programming · Building & coding

lua

/lua

Expert Lua programming assistance covering tables, metatables, closures, coroutines, and C embedding. Use when writing game scripts, Neovim plugins, Nginx OpenResty modules, or embedded device logic.

Licence
MIT
Last repository change
01.10.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
1
Deprecation

No signals detected. Repository changed on 01.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Expert Lua programming assistance covering tables, metatables, closures, coroutines, and C embedding. Use when writing game scripts, Neovim plugins, Nginx OpenResty modules, or embedded device logic.

Original author description

Expert Lua programming assistance covering tables, metatables, closures, coroutines, and C embedding. Use when writing game scripts, Neovim plugins, Nginx OpenResty modules, or embedded device logic.

How it works

  1. When to Use
  2. Quick Start
  3. Core Concepts
  4. Common Patterns
  5. Best Practices
  6. Troubleshooting
  7. References
Original SKILL.md headings · 145 lines, 599 words
SKILL.md preview
---
name: lua
description: Expert Lua programming assistance covering tables, metatables, closures, coroutines, and C embedding. Use when writing game scripts, Neovim plugins, Nginx OpenResty modules, or embedded device logic.
---

# Lua

A powerful, efficient, lightweight, embeddable scripting language.

## When to Use

- **Game Engine Scripting & Modding**: Scripting gameplay logic, AI behavior, and UI in Roblox, World of Warcraft, and LÖVE2D.
- **High-Performance Web Gateways (Nginx / OpenResty)**: Executing microsecond request routing, auth token checks, and WAF rules in Nginx.
- **Redis Server-Side Scripting**: Running atomic transactions and complex data manipulations directly inside Redis instances.
- **Extensible Editor Configurations (Neovim)**: Writing custom editor plugins, keymaps, and LSP hooks in modern Neovim.

## Quick Start

```lua
print("Hello, World!")

function factorial(n)
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end
```

## Core Concepts

### Tables as Universal Data Structures

Lua represents arrays, hashes, objects, modules, and namespaces using a single unified primitive: the Table:

```lua
-- Array style (1-indexed!)
local fruits = {"Apple", "Banana", "Cherry"}
print("First item:", fruits[1])

-- Hash / Dictionary style
local config = {
  host = "127.0.0.1",
  port = 8080,
  ["max-connections"] = 100
}

-- Iterating key-value pairs
for key, value in pairs(config) do
  print(key .. " = " .. tostring(value))
end
```

### Metatables & Object-Oriented Inheritance

Overloads table behaviors (operators, indexing, custom methods) via metamethods:

```lua
local Account = {}
Account.__index = Account

function Account.new(balance)
  local self = setmetatable({}, Account)
  self.balance = balance or 0
  return self
end

function Account:deposit(amount)
  self.balance = self.balance + amount
  return self.balance
end

local acc = Account.new(100)
acc:deposit(50)
print("Account balance:", acc.balance) -- 150
```

### Coroutines for Cooperative Concurrency

Fibers with manual yield and resume control:

```lua
local co = coroutine.create(function()
  for i = 1, 3 do
    print("Coroutine yield:", i)
    coroutine.yield()
  end
end)

coroutine.resume(co) -- "Coroutine yield: 1"
coroutine.resume(co) -- "Coroutine yield: 2"
```

## Common Patterns

### Object-Oriented Class via Metatables

**Problem**: Lua lacks a native `class` keyword; object models must be built from first principles.

**Solution**:
Use metatables with the `__index` metamethod:

```lua
local Vector2 = {}
Vector2.__index = Vector2

function Vector2.new(x, y)
    local self = setmetatable({}, Vector2)
    self.x = x or 0
    self.y = y or 0
    return self
end

function Vector2:magnitude()
    return math.sqrt(self.x * self.x + self.y * self.y)
end

local v = Vector2.new(3, 4)
print("Magnitude:", v:magnitude()) -- Output: 5
```

## Best Practices

**Do**:

- Always Declare Variables as `local`: Global variables by default pollute `_G` and degrade performance; use `local var = ...`.
- Remember Lua Arrays are 1-Indexed: Arrays start at index `1`, not `0`.
- Use LuaJIT where Supported: Leverage the LuaJIT compiler for near C-speed execution in OpenResty and games.
- Cache Global Function Lookups: Localize hot functions (`local sub = string.sub`) in tight loops to eliminate global table lookups.

**Don't**:

- Store `nil` values in arrays: Storing `nil` inside sequential tables breaks length operator (`#`) calculations.
- Create circular metatable references: Infinite recursion during index lookups causes stack overflow errors.
- Concatenate strings repeatedly in loops: Use `table.insert` and `table.concat` to avoid O(N^2) memory reallocations.

## Troubleshooting

| Error                                                           | Cause                                                           | Solution                                                                  |
| :-------------------------------------------------------------- | :-------------------------------------------------------------- | :------------------------------------------------------------------------ |
| `attempt to index a nil value`                                  | Accessing key on a variable that evaluated to `nil`.            | Guard access with `if tbl and tbl.key then` or assign default table `{}`. |
| `attempt to call a nil value (method call with . instead of :)` | Calling method with `v.magnitude()` instead of `v:magnitude()`. | Use colon syntax `:` to automatically pass `self` as the first argument.  |
| `1-based indexing confusion`                                    | Treating first index in Lua array tables as `0`.                | Access first element with `tbl[1]` and iterate with `ipairs`.             |

## References

- [Lua 5.4 Reference Manual](https://www.lua.org/manual/5.4/)

Versions

RepositoryScoreLicence
This page · G1Joshi/Agent-Skills100MIT
dkbnull/hello-skill86MIT

Database snapshot: 13.11.2026 · package version 06b2ee5aafae (SHA-256 digest of the entire package)

Downloads in the last 30 days: 1 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 90lua-guide
    Use when editing general-purpose Lua 5.4+: modules, scripts, configuration. Triggers on `.lua` files and prompts about module patterns, local scoping, metatables, coroutines, error handling, even when the user doesn't say 'Lua'.
  • 100lua-guide
    Lua language guardrails, patterns, and best practices for AI-assisted development. Use when working with Lua files (.lua), or when the user mentions Lua/LuaJIT/Neovim/Love2D. Provides table patterns, metatable guidelines, coroutine usage, and embedding conventions specific to…
  • 85Lua C Integration
    Use when lua C API for extending Lua with native code including stack operations, calling C from Lua, calling Lua from C, creating C modules, userdata types, metatables in C, and performance optimization techniques.
  • 100neovim
    Expert Neovim assistance covering Lua configuration, native LSP client, Treesitter syntax highlighting, and plugin management via lazy.nvim. Use when configuring Neovim, writing init.lua, setting up LSP keymaps, managing plugins, or optimizing editor startup performance.
  • 81Lua Tables Patterns
    Use when lua tables as the universal data structure including arrays, dictionaries, objects, metatables, object-oriented patterns, data structures, and advanced table manipulation for building flexible, efficient Lua applications.
  • 100lua-helper
    Current Lua guidance for portable Lua, LuaJIT, Neovim's Lua interface, WezTerm configuration, LuaLS, formatting, linting, testing, and security. Use when writing or reviewing Lua, Neovim plugins/config, WezTerm config, rockspecs, or Lua tooling.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.