Essential Objects
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. |
Basic math functions
Operation |
Description |
|
|
|
|
|
|
|
|
|
Remainder of a
divided by b
. |
Function |
Description |
|
Returns random number from 1
to n
(no negatives). |
|
Returns random number from a
to b
. |
|
Returns the largest number. |
|
Returns the smallest number. |
|
|
|
|
|
|
|
|
|
|
String functions
Operation |
Description |
|
Combine two strings. |
Function |
Description |
|
|
|
Returns str
in upper-case. |
|
Returns str
in lower-case. |
|
|
|
Returns str
repeated n
times |
|
Return sub-string of str
from a
to b
. |
Tables
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)
|
Tables are a collection of values. They are defined using curly braces {} with values separated by commas. Access the values inside using square brackets []. Tables are sometimes called arrays. Use a for loop to work with all items in a table individually. The :GetChildren() method returns a table of children in an object.
|
|
Constants
|
Parent of all game services. |
|
Container for all bricks and models are stored. |
|
The currently running script. |
Finding Objects
workspace.Part:Destroy()
print(script.Parent.Name)
game.ServerStorage.Tree:Clone()
|
Use a period to access an object's children. Use .Parent to access an object's parent. Use constants like game, workspace, and script to identify objects in the hierarchy.
Creating objects
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?
|
How do I copy a preexisting object? Using object:Clone()
and setting the parent: newTree = workspace.Tree:Clone() newTree.Parent = workspace
|
General Object Functions
Method name |
Description |
|
Return a child with name
or nil
if it doesn't exist. |
|
Pauses until a child with a name exists and returns it. |
|
Return whether the object is a certain type of object. |
|
Makes and returns a copy of an object. |
|
Permanently delete an object. |
|
Return a list of an object's children. |
Event basics
function onTouch(part)
print(part.Name .. " touched me!")
end
workspace.Part.Touched:connect(onTouch)
|
Events are specific occurrences relating to objects. When an event fires, or occurs, all connected functions are called.
|
|
Basic functions
|
Wait n
seconds then continue. |
|
Display something in the Output window. |
Variables
local myScore = 5
myScore = myScore + 1
print(myScore)
local myName = "Ozzy"
print("My name is " .. myName)
|
Variables store data of any kind - numbers, strings, tables, objects or nil (nothing). A local variable is only accessible in the block of code it is defined in.
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
|
If statements will run their code if the value between if/then is true (or not nil). They can one an else block, or any number of elseif blocks.
Loops
Numeric for loop For counting numerically. Example: Count from 1
to 5
: for i = 1, 5 do print(i) end
|
Generic for loop Most often used for object children. Example: Print all children in object: for i, child in pairs(object:GetChildren()) do print(child.Name) end
|
While loop Perform code until a condition is false. Example: Remove all children named 'Ball' while object:FindFirstChild("Ball") do object.Ball:Destroy() end
|
Repeat-until loop Perform code once, then again until a condition is true. Ex.: Copy objects until there are 5. repeat newObject = object:Clone() newObject.Parent = workspace wait(1) until #workspace:GetChildren() >= 5
|
Loops are used to iterate, or repeat code a number of times.
Function examples
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)
|
A function is a named block of code that can be run anywhere in code by calling it by name. Functions can have arguments (given values) and/or return values.
|
Created By
ozzypig.com
Metadata
Comments
Ozzypig, 07:41 25 Jan 16
First draft published, I hope to get some good feedback on my first cheat sheet!
[deleted], 00:17 28 Jan 16
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!
Ozzypig, 17:17 25 Jan 16
Making some minor fixes and changes as I receive feedback.
speelpijn 21:27 26 Jan 16
what is child?
xCodeLua 21:27 26 Jan 16
nice, clean, and basic. good job.
UndeniableLimited 21:27 26 Jan 16
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!
vach 21:27 26 Jan 16
Something like this for searching instances would be really good.
kaiHirotsku 21:27 26 Jan 16
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.
XxmanwolfxX 09:23 15 Feb 16
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?
Nik1080 16:09 16 Sep 16
I am a beginning scripter, and this is an awesome help! Thank you!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by Ozzypig