Basic Scripting
Introduction
This article is an easy way to get started scripting. This will teach you how to script simple scripts that trigger when you touch them from the start.
Requirements
This tutorial is for people familiar with ROBLOX, and know ROBLOX Studio very well (a few weeks of experience). They should be familiar with the explorer, properties, and the output.
Important Things To Remember When Scripting
Spelling is very important when scripting. If you misspell something, your script will not work properly. Most of the time, the output will catch these simple syntax (spelling) errors. To open the output window go to view-->Output.
Before Starting
Before you can script, you must have something to put your script in. I suggest finishing your map before starting any scripting. To start out, we will make a simple door that opens by a button. After you make the door and button, group it and name the door "Door" and the button "Button." Then you have to insert a script into the button. To do that select the button, then in the menu, do Insert -> Object -> "Script". Now you have to open it. While in Explorer, select the button again, click the plus sign, and double click on the Script. All that should be there is print("Hello World!"), which is the default text that you should remove. Then, you can start actually scripting!
WARNING ! If the door is composed of more than one brick, you must name all the bricks.
Lines To Remember
These are lines that are best to remember, you will use these in many scripts. This is the first major line you will use,
local function onTouched(otherPart)
which is the function line for code that uses the Touched event.
Note again that the spacing and capitalization are very important. The next line is usually the last line used in scripts using the Touched event:
script.Parent.Touched:connect(onTouched)
This will make the function run when the script's Parent, the detector, is touched. Don't forget these lines, as an error in one of these is common.
What Scripts Can Do
If one looks in the properties window while having a brick selected one will see several variables such as: BrickColor, Size, CanCollide, and Transparency. Anything in the properties window can be altered in a script.
To make the door invisible, change the Transparency property:
script.Parent.Parent.Door.Transparency = 1
NOTE: Remember that script.Parent represents the button. The parent of the button is the grouped model - which contains the bricks called Door and Button. So script.Parent.Parent is refering to the model. The next step is script.Parent.Parent.Door which will give you access to the brick called Door.
To make a door such that one can walk through it, change the CanCollide property:
script.Parent.Parent.Door.CanCollide = false
How do you know if you use numbers or True/False? If the property has a checkbox, then it is true or false, true being checked, and false being unchecked. If it is a value, like transparency is, then it is a number between 0 and 1. If it has a drop down menu, like BrickColor, then it has a value for each item in the menu. Usually the values are pretty random, starting at 0. BrickColor codes are in this page: Scripting.
So, our script so far is:
local function onTouched(otherPart)
script.Parent.Parent.Door.Transparency = 1
script.Parent.Parent.Door.CanCollide = false
script.Parent.Touched:connect(onTouched)
But, we forgot one thing. Ends! You need an end to finish the functions and other pieces of code. We need one end to finish every function. So, if you have 5 functions, you will have 5 ends. The ends go right before the ending line, like this.
local function onTouched(otherPart)
script.Parent.Parent.Door.Transparency = 1
script.Parent.Parent.Door.CanCollide = false
end
script.Parent.Touched:connect(onTouched)
But we still need to make the door solid again after a while, so we will add a wait function by using this line:
wait(3)
That waits 3 seconds before going to the next line. Now to return the door to its original state:
local function onTouched(otherPart)
script.Parent.Parent.Door.Transparency = 1 -- an invisible door
script.Parent.Parent.Door.CanCollide = false -- a door that you can walk through
wait(3)
script.Parent.Parent.Door.Transparency = 0.3 -- a partially visible door
script.Parent.Parent.Door.CanCollide = true -- a door you can't walk through anymore
end
script.Parent.Touched:connect(onTouched)
That is a working door, but there are shortcuts to make it easier. You can see a lot of repetition ("script.Parent.Parent.Door") so we can make that easier.
WARNING: If your door is a group of 2 bricks (or more), called e.g., "Door", which is a group of the bricks BodyA and BodyB:
local function onTouched(hit)
script.Parent.Parent.Door.BodyA.Transparency = 1
script.Parent.Parent.Door.BodyB.Transparency = 1
script.Parent.Parent.Door.BodyA.CanCollide = false
script.Parent.Parent.Door.BodyB.CanCollide = false
wait(3)
script.Parent.Parent.Door.BodyA.Transparency = 0.3
script.Parent.Parent.Door.BodyB.Transparency = 0.3
script.Parent.Parent.Door.BodyA.CanCollide = true
script.Parent.Parent.Door.BodyB.CanCollide = true
end
script.Parent.Touched:connect(onTouched)
Every brick that composes the door must be scripted.
Another example
Before the local function onTouched(otherPart) line, define the door with this line:
local door = script.Parent.Parent.Door
If we have that, we can shorten the script:
local door = script.Parent.Parent.Door
local function onTouched(otherPart)
door.Transparency = 1
door.CanCollide = false
wait(3)
door.Transparency = 0.3
door.CanCollide = true
end
script.Parent.Touched:connect(onTouched)
WARNING : With a door of 2 bricks called for the example Body A and BodyB
local door = script.Parent.Parent.Door
local function onTouched(otherPart)
door.BodyA.Transparency = 1
door.BodyB.Transparency = 1
door.BodyA.CanCollide = false
door.BodyB.CanCollide = false
wait(3)
door.BodyA.Transparency = 0.3
door.BodyB.Transparency = 0.3
door.BodyA.CanCollide = true
door.BodyB.CanCollide = true
end
script.Parent.Touched:connect(onTouched)
Explosions, Messages, and More!
You can even insert things like, Explosions, Messages, Values, and more. A full list of what you can insert is here: Class reference. The most common ones, however, are:
- IntValue
- Message
- Explosion
With the things you insert you have to define parts to it. Try inserting these things while in studio to see what you have to define. With the Message you have to define:
- Parent
- Text
Explosion:
- Parent
- BlastRadius
- BlastPressure
- Position
IntValue:
- Parent
- Value
Let's make a message block. We'll start with the lines to remember:
local function onTouched(otherPart)
end
script.Parent.Touched(onTouched)
Now, to insert the message object:
local message = Instance.new("Message")
Now, the things we have to define are Parent and Text:
message.Text = "This is the sample message text."
message.Parent = workspace
We can also, make it more simple like this:
local message = Instance.new("Message")
message.Text = "This is the sample message text."
message.Parent = workspace
But we need to make it go away, after 3 seconds. The Destroy() method will remove the message from the screen. Remember to put a colon between message and Destroy:
wait(3)
message:Destroy()
So, now the whole thing:
local function onTouched(otherPart)
local message = Instance.new("Message")
message.Text = "This is the sample message text."
message.Parent = workspace
wait(3)
message:Destroy()
end
script.Parent.Touched:connect(onTouched)
If you try to touch this, you will see a bunch of messages because the onTouched function will keep going over and over. To fix this, use Debounce.
If you use debounce, it will look like this:
local debounce = false
local function onTouched(otherPart)
if not debounce then
debounce = true
local message = Instance.new("Message")
message.Text = "This is the sample message text."
message.Parent = workspace
wait(3)
message:Destroy()
debounce = false
end
end
script.Parent.Touched:connect(onTouched)
Loops
Loops are what you use to make things repeat; Instead of copying and paste dozens of times. There are 3 different types of loops.
- While true do
- For loop
- Repeat loop
While true do
While loops need ends to show where they go back to the beginning. We only need one end here too because there is no function. Here's the whole script:
while true do
wait(1) -- change this time to however many seconds you DON'T want there to be a message
local message = Instance.new("Message")
message.Text = "Please send me a friend request!"
message.Parent = workspace
wait(300) -- change this time to however many seconds you want the message to remain
message:Destroy()
end
For loop
"for" is used to make something go a set number of times. Unlike while true do the makes it go constantly. For Example:
local T = 5 -- How many times it does it.
for i = 1, T, 1 do
local part = Instance.new("Part")
part.Parent = workspace
end
The 'for' sets i initially to 1 then adds 1 to i after each iteration has passed until i is equal to T. This would insert a part into the work space 5 times.
Getting Children
It is possible to use "for" to get to children of anything, e.g.,:
local children = workspace:GetChildren()
for i, v in ipairs(children) do
if v.ClassName == "Part" then
v.Reflectance = 1
end
end
Repeat loops
This way to make something loop is rarely used. You can use a for loop do do anything that repeat can do but repeat is simpler. For Example:
local numberValue = Instance.new("NumberValue")
repeat
numberValue.Value = numberValue.Value + 1
until (numberValue.Value == 8)
print("It stops at 8")