Online Lua Editor and Code Runner
Free online Lua editor with real-time execution, console output, and standard library support. Perfect for learning Lua, testing scripts, and practicing game development.
Loading editor...
Features
Lua Execution
Execute Lua code directly in your browser
Console Output
Real-time console output with print support
Standard Library
Access to Lua's standard library
Error Handling
Clear error messages with line numbers
Metatable Support
Full metatables and metamethods support
Code Sharing
Share Lua code snippets with others
Frequently Asked Questions
How to get started with Lua?
Let's start with Lua basics:
-- Basic output
print("Hello, World!")
-- Variables and types
local name = "Lua"
local number = 42
local isTrue = true
-- String concatenation
print("Value: " .. tostring(number))
-- Functions
function greet(name)
print("Hello, " .. name)
end
greet("Programmer")
Our editor provides real-time execution and feedback.
How to work with tables in Lua?
Learn Lua's powerful table data structure:
-- Dictionary-style table
local person = {
name = "John",
age = 30,
skills = {"Lua", "Python"}
}
-- Accessing values
print(person.name) -- Using dot notation
print(person["age"]) -- Using bracket notation
-- Array-style table
local numbers = {1, 2, 3, 4, 5}
-- Iterating through tables
for i, v in ipairs(numbers) do
print(i, v)
end
-- Table length
print("Length:", #numbers)
Practice these table operations in our editor.
How to use Lua metatables?
Explore Lua's metatable functionality:
-- Create a simple vector type
local Vector = {}
Vector.__index = Vector
function Vector.new(x, y)
return setmetatable({x = x, y = y}, Vector)
end
-- Add metamethod for addition
Vector.__add = function(a, b)
return Vector.new(a.x + b.x, a.y + b.y)
end
-- Create and use vectors
local v1 = Vector.new(1, 2)
local v2 = Vector.new(3, 4)
local v3 = v1 + v2
print(v3.x, v3.y) -- Output: 4 6
Common metamethods:
__index
: Table lookup__newindex
: Table update__call
: Function call__add
,__sub
, etc.: Arithmetic operations
How to handle errors in Lua?
Learn error handling patterns:
-- Basic error handling with pcall
local status, err = pcall(function()
error("Something went wrong!")
end)
if not status then
print("Error caught:", err)
end
-- Using assert
local function divide(a, b)
assert(b ~= 0, "Division by zero!")
return a / b
end
-- Advanced error handling with xpcall
local function errorHandler(err)
return debug.traceback(err, 2)
end
xpcall(function()
error("Custom error")
end, errorHandler)
Practice error handling in our safe environment.
How to use Lua modules?
Create and use Lua modules:
-- mymodule.lua
local M = {}
-- Public function
function M.greet(name)
print("Hello, " .. name)
end
-- Private function
local function internal()
print("This is private")
end
-- Module initialization
function M.init()
internal()
return true
end
return M
Using the module:
local mymodule = require("mymodule")
mymodule.init()
mymodule.greet("Lua")
Test modular code in our editor.
How to work with Lua coroutines?
Understand cooperative multitasking with coroutines:
-- Create a coroutine
local function producer()
for i = 1, 5 do
print("Producing:", i)
coroutine.yield(i)
end
end
local co = coroutine.create(producer)
-- Resume the coroutine
while true do
local status, value = coroutine.resume(co)
if not status then
break
end
print("Consumed:", value)
end
-- Check coroutine status
print("Status:", coroutine.status(co))
Practice cooperative multitasking with our coroutine support.