Learn Lua step-by-step with interactive examples!
In this lesson, we'll cover the basics of Lua, the scripting language used in Roblox Studio. Lua is simple and powerful, making it perfect for game development!
Key Concepts:
Example: Below is a simple Lua script that prints a message:
-- Simple Lua Script Example
local message = "Hello, Roblox!" -- Define a variable
print(message) -- Output the message
In Lua, variables are used to store values that you can reuse in your code.
Example: Below is a simple script that defines a variable and outputs its value:
-- Variables in Lua
local myName = "Max" -- Store a name in a variable
print(myName) -- Output the value of myName
Functions are blocks of code that you can call to perform an action. They allow you to reuse code and keep things organized.
Example: Below is a function that prints a greeting message:
-- Functions in Lua
local function greet(name)
print("Hello, " .. name) -- Concatenate the greeting with the name
end
greet("Max") -- Call the function
Conditionals allow you to make decisions in your code, such as checking if a condition is true or false.
Example: Below is a conditional that checks if a player’s health is above a certain value:
-- Conditionals in Lua
local health = 50
if health > 40 then
print("You are in good health!")
else
print("You need to heal!")
end
Loops allow you to repeat an action multiple times. You can use them for tasks like iterating over items or repeating a block of code.
Example: Below is a loop that prints numbers from 1 to 5:
-- Loops in Lua
for i = 1, 5 do
print(i)
end