Roblox > Scripting Lessons >

The Fast Track (For Experienced Programmers)

Warning: This page is intended for experienced users who already know other programming languages such as Java, Java Script, or C. If you are new to computer programming, read Getting Started instead. If you already have experience with programming however, read on and I'll do what I can to get you started ASAP.

Roblox LUA is a loosely typed object-oriented programming language. I believe it is fairly easy to use, but some elements of it can be confusing to people who have used some of the more common languages. For example, in LUA you cannot use brackets {} to group sections of code together. Rather, declaring an if statement, function (method), or loop automatically starts a block of code, and that block must end with the keyword 'end'. Also note that lines of code do not have to be terminated with a semicolon. (;)

Note, if you need help figuring out how to display the output window or create a new script, read the Getting Started guide and then return to this page to learn the syntax.

Here is a quick reference of the Syntax of Roblox LUA:

If statements:

if x == 5 then
-- Code here
end

For Loops:

for i=1, i<10 do
-- Code here
end

While loops:

while x>3 do
-- Code here
end

Functions (methods):

function foo(var1, var2)
-- Code Here
return var1 + var2 -- Example of a return statement
end


When you declare a variable, the variable's scope is automatically defined as the entire script you are declaring the variable in. If you want to declare a variable with a scope that ends at the end of a block of code, use the keyword local:

local var = 1 -- A variable with a local scope

Single-line comments are defined with two dashes (--), and multi-line comments are started with two dashes and two opening brackets (--[[) and ended with two closing brackets (]]).


Tables (arrays) are declared with opening and closing brackets with coma separated values, like this:

table1 = {"Test", var1, {"Nested table", var2}, test}

You can find the length of a table using the pound sign, like this:

var = #table -- Var equals the length of table 'table'.


There are also some constants you should know about:

script - This constant equals the script object that you are typing in.
game - This constant is the root object that contains everything in the game.


Useful functions:

print() - The print function
wait(x) - Causes the program to wait for x seconds

Referencing Roblox objects usually starts with either the constant script or game. Once you have a Roblox object to work with, you can access its properties or children with a period, like this:

game.Workspace.brick.Transparency = 0.5

You must use a colon when calling a function of an object, not a period. For example:

children = script.Parent:getChildren()

You can also connect events to a function by calling an object's connect function, like this:

game.Workspace.Part.Touched:connect(myFunction)

Well, I think that about covers the basics. I'm sure you'll learn much more than I could ever teach you when you try scripting yourself. Be sure to check out the Roblox wiki or some scripts on the free models. The wiki has a complete reference of all Roblox objects, functions, and events, and looking at examples is a great way to learn. If you still have questions, don't hesitate to PM me or ask on the Roblox forums. Have fun!
Updated on Dec 5, 2011 by Ajedi32 (Version 6)
comments powered by Disqus