Master Lua programming and build educational games for Roblox! Interactive tutorial with live code examples.
Lua is the programming language used by Roblox. It's simple, powerful, and perfect for beginners! With Lua, you can create games, build interactive experiences, and even make money from your creations.
Create any game you can imagine - from obbies to tycoons to educational quizzes!
Popular games can earn thousands of Robux daily. Convert to real money!
200+ million active users. Your game can be played by kids worldwide!
Lua teaches you real programming concepts used in many industries.
Ottodot has built 200+ educational Roblox games! You could build the next one.
Let's start with the fundamentals of Lua programming!
-- This is a comment in Lua -- Comments are ignored by the computer -- 1. Print something to the screen print("Hello, Roblox!") -- 2. Basic math print(10 + 5) -- Output: 15 print(20 - 8) -- Output: 12 print(6 * 7) -- Output: 42 print(100 / 4) -- Output: 25 -- 3. String concatenation (joining text) local name = "Player1" print("Welcome, " .. name .. "!")
Variables store data. Think of them as labeled boxes that hold information.
-- Variables store different types of data -- 1. Strings (text) local playerName = "Budi" local greeting = "Hello!" -- 2. Numbers local score = 100 local health = 50.5 -- 3. Booleans (true or false) local isAlive = true local hasShield = false -- Using variables print("Player: " .. playerName) print("Score: " .. score) -- Changing a variable score = score + 50 print("New Score: " .. score)
โข Use letters, numbers, underscores
โข Must start with a letter or underscore
โข No spaces allowed
โข Case sensitive (score โ Score)
โข Use descriptive names (playerScore > s)
Functions are reusable blocks of code. Write once, use many times!
-- Creating a function local function greet(name) print("Hello, " .. name .. "!") end -- Using the function greet("Budi") -- Output: Hello, Budi! greet("Siti") -- Output: Hello, Siti! -- Function with return value local function add(a, b) return a + b end local result = add(10, 5) print(result) -- Output: 15 -- Game function example local function calculateScore(correct, wrong) local points = (correct * 10) - (wrong * 5) return points end local finalScore = calculateScore(8, 2) print("Final Score: " .. finalScore) -- Output: 70
Tables store multiple values. They're like lists or dictionaries in other languages.
-- Array (list of items) local fruits = {"Apple", "Banana", "Orange"} -- Access items (index starts at 1!) print(fruits[1]) -- Output: Apple print(fruits[2]) -- Output: Banana -- Add item table.insert(fruits, "Mango") -- Loop through array for i, fruit in ipairs(fruits) do print(i .. ". " .. fruit) end -- Dictionary (key-value pairs) local player = { name = "Budi", health = 100, score = 0, level = 1 } -- Access dictionary values print(player.name) -- Output: Budi print(player.health) -- Output: 100 -- Modify values player.score = player.score + 50
In Lua, array indexes start at 1, not 0 like in JavaScript or Python!
Roblox provides built-in services to create amazing games!
-- Get Roblox services local Players = game:GetService("Players") local Workspace = game:GetService("Workspace") -- When a player joins the game Players.PlayerAdded:Connect(function(player) print(player.Name .. " joined the game!") -- Give the player a sword local sword = Instance.new("Tool") sword.Name = "Magic Sword" sword.Parent = player.Backpack end) -- Create a part (block) in the game local part = Instance.new("Part") part.Name = "QuizBlock" part.Position = Vector3.new(0, 10, 0) part.BrickColor = BrickColor.new("Bright blue") part.Parent = Workspace -- Detect when something touches the part part.Touched:Connect(function(hit) local player = Players:GetPlayerFromCharacter(hit.Parent) if player then print(player.Name .. " touched the block!") end end)
Let's build a complete educational quiz game for Roblox!
-- ============================================ -- MATH QUIZ GAME FOR ROBLOX -- ============================================ local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Create RemoteEvent for client-server communication local QuizEvent = Instance.new("RemoteEvent") QuizEvent.Name = "QuizEvent" QuizEvent.Parent = ReplicatedStorage -- Questions database local Questions = { { question = "What is 12 ร 8?", options = {"84", "96", "108", "72"}, answer = 2 -- Index of correct answer (96) }, { question = "What is 45 รท 9?", options = {"4", "5", "6", "7"}, answer = 2 -- Answer: 5 }, { question = "What is 15 + 27?", options = {"40", "42", "44", "46"}, answer = 2 -- Answer: 42 }, { question = "What is 100 - 37?", options = {"53", "63", "73", "83"}, answer = 1 -- Answer: 63 }, { question = "What is 9 ร 7?", options = {"54", "63", "72", "81"}, answer = 2 -- Answer: 63 } } -- Player data storage local PlayerData = {} -- Initialize player when they join local function onPlayerAdded(player) PlayerData[player.UserId] = { score = 0, level = 1, questionsAnswered = 0, correctAnswers = 0 } print("Welcome " .. player.Name .. "!") -- Send first question to player local questionIndex = math.random(1, #Questions) local question = Questions[questionIndex] QuizEvent:FireClient(player, "newQuestion", question) end -- Handle player answers local function onAnswerReceived(player, questionIndex, selectedIndex) local data = PlayerData[player.UserId] local question = Questions[questionIndex] data.questionsAnswered = data.questionsAnswered + 1 if selectedIndex == question.answer then -- Correct answer! data.correctAnswers = data.correctAnswers + 1 data.score = data.score + 10 -- Level up every 5 correct answers if data.correctAnswers % 5 == 0 then data.level = data.level + 1 print(player.Name .. " reached level " .. data.level .. "!") end -- Notify player of success QuizEvent:FireClient(player, "correct", data.score, data.level) else -- Wrong answer QuizEvent:FireClient(player, "wrong", question.answer) end -- Send next question local nextIndex = math.random(1, #Questions) QuizEvent:FireClient(player, "newQuestion", Questions[nextIndex]) end -- Connect events Players.PlayerAdded:Connect(onPlayerAdded) QuizEvent.OnServerEvent:Connect(onAnswerReceived)
Copy this code into Roblox Studio and create your educational quiz game!