Lua for Game Development

Reviewed & published by Brayan K

By the end of this lesson you'll understand the game loop that powers every game, how to move things smoothly with delta time, and how to model players and enemies as tables — the exact pattern Roblox and LÖVE developers use every day.

Part of the free Lua course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn

💡 Real-World Analogy

A game is a flipbook animation. Each page is one still picture — a frame — and flipping through them fast enough (around 60 pages a second) tricks your eye into seeing smooth motion. The game loop is your hand flipping the pages: every flip it reads what the player pressed, nudges everything a little (the update), and draws the new picture (the draw). The catch: some hands flip faster than others. Delta time is how you account for that — instead of "move 5 pixels per page", you say "move 150 pixels per second", so a fast flipper and a slow flipper end up in exactly the same place.

1. Why Lua Rules Game Scripting

Game studios write their heavy machinery — rendering, physics, audio — in fast languages like C++. But they need a small, friendly language to script the actual gameplay: what an enemy does when you get close, what a button does when you press it. Lua is the industry's favourite choice for that job because it's tiny, fast, and easy to embed. You've already learned the whole language; this is where it pays off.

Where Lua Powers Games

Different engines, one language. The game loop and table-based entities you're about to learn are the same idea in all of them — only the function names change.

2. The Game Loop: update and draw

Every game is an infinite loop that, each frame, reads input, updates the world, and redraws the screen. You don't write that loop yourself — the framework runs it and calls your functions at the right moments. In LÖVE there are three: love.load() runs once at startup, love.update(dt) runs every frame to change state, and love.draw() runs every frame to paint it. The dt handed to update is delta time — the seconds elapsed since the last frame — and it's the key to smooth movement.

-- The GAME LOOP is the heartbeat of every game. Each frame it does
-- three things in order: read input, UPDATE the world, then DRAW it.
-- LÖVE (the popular Lua game framework, written "LÖVE2D") gives you
-- three callbacks to fill in — it calls them for you ~60 times a second.

function love.load()
  -- Runs ONCE at startup. Set up your starting state here.
  player = { x = 400, y = 300, speed = 200 }   -- a table = one entity
end

function love.update(dt)        -- dt = "delta time": seconds since the last frame
  -- Move right while the right arrow is held down.
  if love.keyboard.isDown("right") then
    player.x = player.x + player.speed * dt     -- * dt keeps speed frame-rate-independent
  end
  if love.keyboard.isDown("left") then
    player.x = player.x - player.speed * dt
  end
end

function love.draw()
  -- Draw a filled circle at the player's position, radius 20.
  love.graphics.circle("fill", player.x, player.y, 20)
end

-- Behaviour: a circle slides left/right at 200 pixels per second,
-- moving the same real-world distance whether the game runs at 30 or 144 fps.

The golden rule lives on the movement line: player.x = player.x + player.speed * dt. Because speed is measured in pixels per second and dt is seconds, multiplying them gives pixels this frame. Drop the * dt and your game speeds up on fast computers — the single most common beginner bug in game code.

3. Position, Velocity & Simple Physics

Most movement boils down to two pieces of data per object: its position (where it is, as x and y) and its velocity (how fast that position changes, also as x and y). Each frame you nudge the position by the velocity, scaled by dt. Add a constant downward velocity each frame and you've built gravity. A vector here is nothing fancy — just a little table with an x and a y.

-- POSITION + VELOCITY is the simplest physics there is. A position is
-- "where you are" (x, y); a velocity is "how fast x and y are changing".
-- Each frame:  new position = old position + velocity * dt.

-- A reusable 2D vector built from a plain table.
local function vec(x, y) return { x = x, y = y } end

local ball = {
  pos = vec(0, 100),     -- starts at the left edge, 100px down
  vel = vec(60, 0),      -- moving right at 60 px/s, not moving vertically
}

local gravity = vec(0, 90)   -- pulls 90 px/s DOWNWARD every second (y grows down)

-- Simulate 3 frames of a 0.5-second step so you can read the numbers.
local dt = 0.5
for frame = 1, 3 do
  -- 1) gravity changes the velocity
  ball.vel.x = ball.vel.x + gravity.x * dt
  ball.vel.y = ball.vel.y + gravity.y * dt
  -- 2) velocity changes the position
  ball.pos.x = ball.pos.x + ball.vel.x * dt
  ball.pos.y = ball.pos.y + ball.vel.y * dt
  print(string.format("frame %d: pos=(%.1f, %.1f) vel=(%.1f, %.1f)",
        frame, ball.pos.x, ball.pos.y, ball.vel.x, ball.vel.y))
end

-- Behaviour (prints):
-- frame 1: pos=(30.0, 122.5) vel=(60.0, 45.0)
-- frame 2: pos=(60.0, 167.5) vel=(60.0, 90.0)
-- frame 3: pos=(90.0, 235.0) vel=(60.0, 135.0)
-- The ball drifts right at a steady 60 px/s while falling faster and faster.

4. Entities as Tables

In a real game you have many objects — a player, enemies, bullets. Each one is a table holding its own fields (its components: position, health, and so on), and you keep them all in one list so the game loop can update and draw them together. Watch the indexing carefully: Lua lists are 1-based, so the first entity is at index 1, and ipairs walks them in order starting there.

-- ENTITIES AS TABLES: every game object is just a table of fields,
-- and you keep them all in one list so the loop can update them together.
-- Remember: Lua tables (lists) are 1-BASED — the first entity is [1].

local entities = {}   -- our list of game objects

-- A tiny factory: build an entity table and add it to the list.
local function spawn(kind, x, y, hp)
  local e = { kind = kind, x = x, y = y, hp = hp, alive = true }
  table.insert(entities, e)             -- appends at position #entities + 1
  return e
end

spawn("Player", 0, 0, 100)
local goblin = spawn("Goblin", 50, 0, 30)
spawn("Goblin", 80, 10, 30)

-- Apply damage to one entity; mark it dead at 0 hp.
local function damage(e, amount)
  e.hp = e.hp - amount
  if e.hp <= 0 then e.alive = false end
end

damage(goblin, 30)        -- 30 - 30 = 0, so goblin.alive becomes false

-- ipairs walks the list in order from index 1; count who is still alive.
local alive = 0
for index, e in ipairs(entities) do
  if e.alive then alive = alive + 1 end
end
print("Entities still alive:", alive)   -- prints: Entities still alive:	2

5. Beyond LÖVE: a Roblox Script

The same skills carry to other engines — only the API differs. Roblox uses Luau and an event-driven style: instead of one big update, you connect functions to events like "a player joined". Here's a classic Roblox server script that hands every new player a coin counter. Notice it's the same Lua you know — tables, functions, string concatenation — wrapped around Roblox's objects.

-- Outside LÖVE, Lua powers ROBLOX (via "Luau", its dialect), Defold,
-- Garry's Mod, and World of Warcraft addons. Here is a Roblox-style
-- server Script: give every player who joins a coin counter.

local Players = game:GetService("Players")

-- PlayerAdded fires once per player; we connect a function to that event.
Players.PlayerAdded:Connect(function(player)
  local stats = Instance.new("Folder")   -- a container object
  stats.Name = "leaderstats"             -- this exact name shows a leaderboard
  stats.Parent = player

  local coins = Instance.new("IntValue") -- holds a whole number
  coins.Name = "Coins"
  coins.Value = 0
  coins.Parent = stats

  print(player.Name .. " joined — gave them 0 coins")
end)

-- Luau also adds string interpolation with backticks and {} :
-- local name = "Sam"
-- print(`Welcome, {name}!`)   -- Luau only; prints: Welcome, Sam!

-- Behaviour: the moment a player joins, a "Coins" value starting at 0
-- appears on the in-game leaderboard, and a join message is printed.

6. 🎯 Your Turn

Now you fill in the gaps. Replace each ___ using the -- 👉 hint, then run the code and check it against the -- ✅ Expected comment. The first exercise nails the delta-time rule; the second nails 1-based entity loops — the two patterns this whole lesson rests on.

-- 🎯 YOUR TURN — make movement frame-rate-INDEPENDENT.
-- A player should move 150 pixels every second no matter the frame rate.
-- That means: position changes by speed * dt each frame.

local player = { x = 0, speed = 150 }

function love.update(dt)
  if love.keyboard.isDown("right") then
    -- 1) Move the player right by speed scaled by delta time.
    player.x = player.x + ___          -- 👉 multiply player.speed by dt
  end
end

-- ✅ Expected: after holding "right" for exactly 1 second of real time,
--    player.x has increased by 150 (because 150 * (sum of dt) = 150).

Next, count the survivors in an entity list the idiomatic, 1-based way:

-- 🎯 YOUR TURN — count living enemies the 1-based Lua way.
local enemies = {
  { name = "Bat",     alive = true  },
  { name = "Slime",   alive = false },
  { name = "Skeleton", alive = true },
}

local alive = 0
-- 1) Loop the list IN ORDER starting at index 1 (hint: ipairs).
for _, e in ___(enemies) do            -- 👉 the iterator that starts at index 1
  -- 2) Only count the ones whose alive field is true.
  if ___ then                          -- 👉 e.alive
    alive = alive + 1
  end
end

print("Alive:", alive)
-- ✅ Expected output:  Alive:	2

Pro Tip

When you remove entities mid-loop (a dead enemy, an off-screen bullet), iterate the list backwards with for i = #list, 1, -1 do. Removing while going forwards shifts later items down and makes the loop skip one — going backwards sidesteps that entirely.

Common Errors (and the fix)

Quick Reference

TaskCode (LÖVE)When it runs
Set up statefunction love.load() endonce
Update worldfunction love.update(dt) endevery frame
Draw screenfunction love.draw() endevery frame
Key held?love.keyboard.isDown("up")in update
Smooth movex = x + speed * dtin update
Add entitytable.insert(list, e)any time
Loop entitiesfor _, e in ipairs(list)1-based

Mini-Challenge: A Bouncing Ball

No blanks this time — just a brief and an outline. Write the whole thing yourself with plain Lua (so you can run it anywhere and print the positions), then check it against the expected first line. This is the same position + velocity loop a real game uses, minus the graphics.

-- 🎯 MINI-CHALLENGE: a bouncing ball (no LÖVE needed — just print it)
-- 1. Make a ball table with  x = 0, y = 0  and  vx = 50, vy = 30  (px/s).
-- 2. Use a fixed step:  local dt = 0.1
-- 3. Loop 5 times. Each step:
--      - add vx * dt to x, and vy * dt to y   (move by velocity * dt)
--      - if y goes above 10, bounce: set vy = -vy   (flip vertical speed)
-- 4. print(x, y) each step so you can watch it travel and bounce.
--
-- ✅ Expected (first line): 5.0	3.0   then x keeps rising by 5 each step.

-- your code here

Where to Go From Here

Free resources to build real games

Project ideas to practise on

LevelProject
BeginnerPong, Snake, Flappy Bird clone
IntermediateSide-scrolling platformer, top-down shooter
AdvancedRoblox obby, tower defence, procedural dungeon

🎉 Course Complete!

Practice quiz

Each frame, the game loop reads input, then does what next?

  • Draws, then updates
  • Updates the world, then draws
  • Sleeps
  • Saves the game

Answer: Updates the world, then draws. The loop updates the world, then draws it, every frame.

What does the dt passed to love.update mean?

  • Draw time
  • Display type
  • Delta time: seconds since the last frame
  • Data table

Answer: Delta time: seconds since the last frame. dt is delta time, the seconds elapsed since the previous frame.

Why multiply movement by dt ?

  • To save memory
  • To slow the game down
  • To round positions
  • To make movement frame-rate-independent

Answer: To make movement frame-rate-independent. Scaling by dt keeps speed in units-per-second regardless of frame rate.

Which LÖVE callback runs once at startup?

  • love.load()
  • love.start()
  • love.update()
  • love.init()

Answer: love.load(). love.load runs once to set up the starting state.

How is a game entity usually represented in Lua?

  • A class instance only
  • A table of fields
  • A global variable
  • A string

Answer: A table of fields. Entities are plain tables holding fields like x, y, and hp.

Which dialect does Roblox use for its game logic?

  • LuaJIT
  • MoonScript
  • Luau
  • Pico-8 Lua

Answer: Luau. Roblox scripts are written in Luau, its Lua dialect.

At what index does the first entity in a Lua list sit?

  • 0
  • -1
  • Any
  • 1

Answer: 1. Lua tables are 1-based, so the first entity is at index 1.

Which iterator walks an entity list in order from index 1?

  • ipairs
  • pairs
  • next
  • each

Answer: ipairs. ipairs visits integer keys 1, 2, 3... in order.

Where should you set up starting state like the player table?

  • In love.update
  • In love.draw
  • In love.load
  • In a global at the top

Answer: In love.load. Initialise once in love.load, not in update (which runs every frame).

When removing entities during iteration, you should loop...

  • Forwards with ipairs
  • Twice
  • In any order
  • Backwards with for i = #list, 1, -1

Answer: Backwards with for i = #list, 1, -1. Looping backwards avoids skipping items as later ones shift down.

Continue this course

Frequently asked questions

Which game engines and games actually use Lua?

A lot of them. Roblox runs Luau (its Lua dialect) for all game logic, LÖVE (Love2D) and Defold are full Lua game frameworks, World of Warcraft uses Lua for its UI addons and macros, and Garry's Mod is scripted in Lua. Learning Lua gives you a direct path into all of these.

What is delta time (dt) and why do I keep multiplying by it?

Delta time is the number of seconds that passed since the previous frame. Frame rates vary between machines and even moment to moment, so if you move by a fixed amount each frame your game runs faster on faster computers. Multiplying movement by dt expresses speed in 'units per second', which stays constant at any frame rate.

How do I represent a game object like a player or enemy in Lua?

As a table. Lua has no classes built in, so an entity is just a table of fields such as { x = 0, y = 0, hp = 100, alive = true }. You usually store all your entities in one list and loop over it each frame with ipairs to update and draw them together.

Why does my Lua entity loop skip the first item or behave oddly?

Lua tables are 1-based: the first element is at index 1, not 0 like most languages. If you write 'for i = 0, #list' or assume index 0 exists, you read a nil and things break. Use 'for i = 1, #list' or, better, 'for _, e in ipairs(list)' which walks the list in order from index 1.

Is the Lua I learned in this course the same as Roblox's Luau?

Yes — Luau is a superset of Lua 5.1. Everything you have learned (tables, functions, loops, metatables, the game loop pattern) works in Luau unchanged. Luau just adds extras on top, like optional type annotations and string interpolation with backticks, so your skills transfer straight across.

Related lessons