General reference for scripting in Lua on Roblox. This cheat sheet is intended for beginner scripters.
|
Class
|
Description
|
|
Part
|
A physical brick in the world.
|
|
Model
|
A container for Parts.
|
|
Folder
|
A container for Scripts and value objects.
|
|
Script
|
A container for Lua source code.
|
|
LocalScript
|
A Script that runs its code on a client.
|
|
Operation
|
Description
|
a + b |
Adds
a and b. |
a - b |
Subtract
a and b. |
a * b |
Multiply
a and b. |
a / b |
Divides
a by b. |
a % b |
Remainder of
a divided by b. |
|
Function
|
Description
|
math.random(n) |
Returns random number from
1 to n (no negatives). |
math.random(a, b) |
Returns random number from
a to b. |
math.max(...) |
Returns the largest number.
|
math.min(...) |
Returns the smallest number.
|
math.floor(n) |
Rounds
n down. |
math.ceil(n) |
Rounds
n up. |
math.abs(n) |
Returns absolute value of
n. |
math.sqrt(n) |
Returns square root of
n. |
math.pi |
Approx equal to
3.14159 |
|
Operation
|
Description
|
a .. b |
Combine two strings.
|
|
Function
|
Description
|
string.len(str) |
Returns length of
str. |
string.upper(str) |
Returns
str in upper-case. |
string.lower(str) |
Returns
str in lower-case. |
string.reverse(str) |
Returns
str in reverse. |
string.rep(str, n) |
Returns
str repeated n times |
string.sub(str, a, b) |
Return sub-string of
str from a to b. |
Name property. Read all string manipulation functions here.
**local** list = {1, 2, 3}
**local** firstNum = list[1]
list[2] = 4
print("There are " .. #list .. " numbers")
**local** total = 0
**for** i = 1, #list **do**
total = total + list[i]
**end**
print("The total is " .. total)
game |
Parent of all game services.
|
workspace |
Container for all bricks and models are stored.
|
script |
The currently running script.
|
workspace.Part:Destroy()
print(script.Parent.Name)
game.ServerStorage.Tree:Clone()
|
How do I create an object?
Using
Instance.new(class) and setting the parent:object.Parent = parent
|
|
How do I access an object's properties?
Use a period (
.):print(object.Name)
|
|
How do I set an object's properties?
Use a period (
.) and equals sign (=):part.Transparency = .5
|
|
How do I destroy an object?
Using
object:Destroy()
|
|
How do I copy a preexisting object?
Using
object:Clone() and setting the parent:newTree = workspace.Tree:Clone()
|
|
Method name
|
Description
|
:FindFirstChild(name) |
Return a child with
name or nil if it doesn't exist. |
:WaitForChild(name) |
Pauses until a child with a name exists and returns it.
|
:IsA(className) |
Return whether the object is a certain type of object.
|
:Clone() |
Makes and returns a copy of an object.
|
:Destroy() |
Permanently delete an object.
|
:GetChildren() |
Return a list of an object's children.
|
**function** onTouch(part)
print(part.Name .. " touched me!")
**end**
workspace.Part.Touched:connect(onTouch)
wait(n) |
Wait
n seconds then continue. |
print(...) |
Display something in the Output window.
|
**local** myScore = 5
myScore = myScore + 1
print(myScore)
**local** myName = "Ozzy"
print("My name is " .. myName)
If statements
**if** workspace:FindFirstChild("Tree") **then**
print("There is a tree here.")
**end**
**if** coins < 5 **then**
print("You need more money.")
**else**
print("You have enough money!")
**end**
**if** player.Name == "Jake" **then**
print("You are an awesome guy, Jake")
**elseif** player.Name == "Sally" **then**
print("You are a sweetheart, Sally")
**else**
print("You are a pretty cool person")
**end**
|
Numeric for loop
For counting numerically.
Example: Count from 1 to 5:for i = 1, 5 do
|
|
Generic for loop
Most often used for object children.
Example: Print all children in object: for i, child in pairs(object:GetChildren()) do
|
|
While loop
Perform code until a condition is false.
Example: Remove all children named 'Ball' while object:FindFirstChild("Ball") do
|
|
Repeat-until loop
Perform code once, then again until a condition is true.
Ex.: Copy objects until there are 5. repeat
|
**function** sayHello()
print("Hello, world")
**end**
sayHello()
**function** addTwoNumbers(a, b)
print("The sum is:", a + b)
**end**
addTwoNumbers(3, 5)
**function** calculateSquare(n)
**return** n * n
**end**
**local** result = calculateSquare(3)
I am a beginning scripter, and this is an awesome help! Thank you!
I love this, even though I have been scripting for a while I will look at this for certain pieces. Thank you so much for making this. Can you possibly add in the keydown events and mouse down events?
First draft published, I hope to get some good feedback on my first cheat sheet!
Nice work, it may be basic but you did just start. I hope you continue with this! Some things to add: How to make color (Color3.new). How to move things (Vector3.new or CFrame.new). If you haven't added function(example). Statements like elseif, then, if, and, or. Ect. Nice work and I hope you continue!
Just realized you made Fisticuffs.Anywho, I hoped for a more in depth description, such as how you can put them together.I'm an amatuer scripter, as well as a game dev, and scripting is my weak point, anywho, thank you for the cheat sheet, may success and fortune be your reward.
Something like this for searching instances would be really good.
This looks great for beginners, I probably won't be using this but if I was just starting to script this would be a great help!
nice, clean, and basic. good job.
what is child?
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.