Basic Scripting

Contents


[edit] 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.

[edit] 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.

[edit] 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.

[edit] 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.

[edit] 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,

function Touched(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(Touched)

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.

[edit] 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:

function onTouched(hit)
	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.

function onTouched(hit)
	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:

function onTouched(hit)
	script.Parent.Parent.Door.Transparency= 1 -- an invisible door
	script.Parent.Parent.Door.CanCollide= false -- a walkthroughable door
	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:

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.

[edit] Another example

Before the function onTouched(hit) line, define the door with this line: {code|=door = script.Parent.Parent.Door}} If we have that, we can shorten the script:

door = script.Parent.Parent.Door
function onTouched(hit)
	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

door = script.Parent.Parent.Door
function onTouched(hit)
	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)

[edit] 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:

  1. IntValue
  2. Message
  3. 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:

  1. Parent
  2. Text

Explosion:

  1. Parent
  2. BlastRadius
  3. BlastPressure
  4. Position

IntValue:

  1. Parent
  2. Value

Let's make a message block. We'll start with the lines to remember:

function onTouched(hit)
end
script.Parent.Touched(onTouched)

Now, to insert the message object: {code|= msg = Instance.new("Message") }} Now, the things we have to define are Parent and Text:

msg.Parent = game.Workspace 
msg.Text = "This is the sample message text."

We can also, make it more simple like this:

msg = Instance.new("Message", Workspace)
msg.Text = "This is the sample message text."

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 msg and Destroy:

wait(3)
msg:Destroy()

So, now the whole thing:

function onTouched(hit)
	msg = Instance.new("Message", Workspace)
	msg.Text = "This is the sample message text."
	wait(3)
	msg: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:

debounce = false
function onTouched(hit)
	if debounce == false then
		debounce = true
		msg = Instance.new("Message", Workspace)
		msg.Text = "This is the sample message text."
		wait(3)
		msg:Destroy()
		debounce = false
	end
end
 
script.Parent.Touched:connect(onTouched)

[edit] 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.

  1. While loop
  2. For loop
  3. Repeat loop

[edit] While loop

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(001) -- change this time to however many seconds you DON'T want there to be a message
	msg = Instance.new("Message")
	msg.Parent = game.Workspace
	msg.Text = "Please send me a friend request!"
	wait(300) -- change this time to however many seconds you want the message to remain
	msg:Destroy()
end

[edit] 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:

T = 5 --How many times it does it.
 
for i = 1, T do
	b = Instance.new("Part")
	b.Parent = game.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.

[edit] Getting Children

It is possible to use "for" to get to children of anything, e.g.,:

a = game.Workspace:GetChildren()
for i = 1, #a do
	if (a[i].className == "Part") then
		a[i].Reflectance = 1
	else return end
end

[edit] 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:

Nv = Instance.new("NumberValue")
repeat
	Nv.value = Nv.value + 1
until (Nv.value == 8)
print("It stops at 8")