Basic Scripting: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
(Instance::Remove is deprecated; changing to Instance::Destroy.)
(Line on repeat loops)
 
(18 intermediate revisions by 3 users not shown)
Line 9: Line 9:
== Requirements ==
== Requirements ==


This tutorial is for people familiar with ROBLOX, and know [[Roblox_Studio|Roblox Studio]] very well (a few weeks of experience). They should be familiar with [http://wiki.roblox.com/index.php?title=Image:Explorerwindow.JPG Explorer], [[Properties]], and [http://wiki.roblox.com/index.php?title=Image:Outputwindow.JPG Output].
This tutorial is for people familiar with ROBLOX, and know [[Roblox_Studio|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 ==
== 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, ''Output'' will catch these simple syntax (spelling) errors. To open the output window go to view-->Output.
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 Starting ==
Line 23: Line 23:
== Lines To Remember ==
== Lines To Remember ==


These are lines that are best to remember, you will use these in most scripts.
These are lines that are best to remember, you will use these in many scripts.
This is the first major line you will use,
This is the first major line you will use,
<pre> function onTouched(hit) </pre>
<syntaxhighlight lang="lua">
which is the ''function'' line for touch scripts.
local function onTouched(otherPart)
</syntaxhighlight>
which is the ''function'' line for code that uses the Touched event.


Note again that the spacing and capitalization are very important.
Note again that the spacing and capitalization are very important.
The next major line is the last one to most scripts:
The next line is usually the last line used in scripts using the Touched event:
<pre> script.Parent.Touched:connect(onTouched) </pre>
<syntaxhighlight lang="lua">
This will make the script activate when the script's ''Parent'' , the button, is touched. Don't forget these lines, as an error in one of these is common.
script.Parent.Touched:connect(onTouched)
</syntaxhighlight>
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 ==
== What Scripts Can Do ==


If one looks in the [[Properties|properties]] window while having a brick selected one will see several variables such as: BrickColor, Size, CanCollide, and Transparency. Anything in the [[Properties|properties]] window can be altered in a script.  
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|properties]] window can be altered in a script.  


To make the door invisible, change the Transparency property:
To make the door invisible, change the Transparency property:
<pre> script.Parent.Parent.Door.Transparency = 1 </pre>
<syntaxhighlight lang="lua">
script.Parent.Parent.Door.Transparency = 1
</syntaxhighlight>


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.  
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:
To make a door such that one can walk through it, change the CanCollide property:
<pre> script.Parent.Parent.Door.CanCollide = false </pre>
<syntaxhighlight lang="lua">
script.Parent.Parent.Door.CanCollide = false
</syntaxhighlight>


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]].
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]].
Line 49: Line 57:
So, our script so far is:
So, our script so far is:


<pre>function onTouched(hit)
<syntaxhighlight lang="lua" line>
local function onTouched(otherPart)
script.Parent.Parent.Door.Transparency = 1
script.Parent.Parent.Door.Transparency = 1
script.Parent.Parent.Door.CanCollide = false
script.Parent.Parent.Door.CanCollide = false
script.Parent.Touched:connect(onTouched)</pre>
 
script.Parent.Touched:connect(onTouched)
</syntaxhighlight>
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.
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.
The ends go right before the ending line, like this.
<pre>function onTouched(hit)
<syntaxhighlight lang="lua" line>
script.Parent.Parent.Door.Transparency= 1
local function onTouched(otherPart)
script.Parent.Parent.Door.CanCollide= false
script.Parent.Parent.Door.Transparency = 1
script.Parent.Parent.Door.CanCollide = false
end
end
script.Parent.Touched:connect(onTouched)</pre>
 
script.Parent.Touched:connect(onTouched)
</syntaxhighlight>
But we still need to make the door solid again after a while, so we will add a ''wait'' function by using this line:
But we still need to make the door solid again after a while, so we will add a ''wait'' function by using this line:
<pre>wait(3)</pre>
<syntaxhighlight lang="lua">
wait(3)
</syntaxhighlight>
That waits 3 seconds before going to the next line. Now to return the door to its original state:
That waits 3 seconds before going to the next line. Now to return the door to its original state:
<pre>function onTouched(hit)
<syntaxhighlight lang="lua" line>
script.Parent.Parent.Door.Transparency= 1 -- an invisible door
local function onTouched(otherPart)
script.Parent.Parent.Door.CanCollide= false -- a walkthroughable door
script.Parent.Parent.Door.Transparency = 1 -- an invisible door
script.Parent.Parent.Door.CanCollide = false -- a door that you can walk through
 
wait(3)
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
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
end
script.Parent.Touched:connect(onTouched)</pre>
 
script.Parent.Touched:connect(onTouched)
</syntaxhighlight>
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.
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:  
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:
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:
<pre>function onTouched(hit)
<syntaxhighlight lang="lua" line>
script.Parent.Parent.Door.BodyA.Transparency= 1
local function onTouched(hit)
script.Parent.Parent.Door.BodyB.Transparency= 1
script.Parent.Parent.Door.BodyA.Transparency = 1
        script.Parent.Parent.Door.BodyA.CanCollide= false
script.Parent.Parent.Door.BodyB.Transparency = 1
        script.Parent.Parent.Door.BodyB.CanCollide= false
    script.Parent.Parent.Door.BodyA.CanCollide = false
        wait(3)
    script.Parent.Parent.Door.BodyB.CanCollide = false
script.Parent.Parent.Door.BodyA.Transparency= 0.3
 
script.Parent.Parent.Door.BodyB.Transparency= 0.3
    wait(3)
        script.Parent.Parent.Door.BodyA.CanCollide= true
 
        script.Parent.Parent.Door.BodyB.CanCollide= true
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
end
script.Parent.Touched:connect(onTouched)</pre>
 
script.Parent.Touched:connect(onTouched)
</syntaxhighlight>
Every brick that composes the door must be scripted.
Every brick that composes the door must be scripted.


== Another example ==
== Another example ==
Before the ''local function onTouched(otherPart)'' line, define the door with this line:
<syntaxhighlight lang="lua">
local door = script.Parent.Parent.Door
</syntaxhighlight>
If we have that, we can shorten the script:
<syntaxhighlight lang="lua" line>
local door = script.Parent.Parent.Door


Before the ''function onTouched(hit)'' line, define the door with this line:
local function onTouched(otherPart)
<pre>door = script.Parent.Parent.Door</pre>
If we have that, we can shorten the script:
<pre>door = script.Parent.Parent.Door
function onTouched(hit)
door.Transparency = 1
door.Transparency = 1
door.CanCollide = false
door.CanCollide = false
wait(3)
wait(3)
door.Transparency = 0.3
door.Transparency = 0.3
door.CanCollide = true
door.CanCollide = true
end
end


script.Parent.Touched:connect(onTouched)</pre>
script.Parent.Touched:connect(onTouched)
</syntaxhighlight>


WARNING : With a door of 2 bricks called for the example Body A and BodyB
WARNING : With a door of 2 bricks called for the example Body A and BodyB
<pre>door = script.Parent.Parent.Door
<syntaxhighlight lang="lua" line>
function onTouched(hit)
local door = script.Parent.Parent.Door
door.BodyA.Transparency= 1
 
door.BodyB.Transparency= 1
local function onTouched(otherPart)
        door.BodyA.CanCollide= false
door.BodyA.Transparency = 1
        door.BodyB.CanCollide= false
door.BodyB.Transparency = 1
        wait(3)
    door.BodyA.CanCollide = false
door.BodyA.Transparency= 0.3
    door.BodyB.CanCollide = false
door.BodyB.Transparency= 0.3
 
        door.BodyA.CanCollide= true
    wait(3)
        door.BodyB.CanCollide= true
 
door.BodyA.Transparency = 0.3
door.BodyB.Transparency = 0.3
    door.BodyA.CanCollide = true
    door.BodyB.CanCollide = true
end
end
script.Parent.Touched:connect(onTouched)</pre>


script.Parent.Touched:connect(onTouched)
</syntaxhighlight>
== Explosions, Messages, and More! ==
== 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:
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
# IntValue
#Message
# Message
#Explosion
# 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:
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
# Parent
#Text
# Text
Explosion:
Explosion:
#Parent
# Parent
#BlastRadius
# BlastRadius
#BlastPressure
# BlastPressure
#Position
# Position
IntValue:
IntValue:
#Parent
# Parent
#Value
# Value
 
Let's make a message block.  We'll start with the lines to remember:
<syntaxhighlight lang="lua" line>
local function onTouched(otherPart)


Lets make a message block.  We'll start with the lines to remember:
<pre>function onTouched(hit)
end
end
script.Parent.Touched(onTouched)</pre>
 
script.Parent.Touched(onTouched)
</syntaxhighlight>
Now, to insert the message object:
Now, to insert the message object:
<pre> msg = Instance.new("Message")</pre>
<syntaxhighlight lang="lua">
local message = Instance.new("Message")
</syntaxhighlight>
Now, the things we have to define are Parent and Text:
Now, the things we have to define are Parent and Text:
<pre> msg.Parent = game.Workspace
<syntaxhighlight lang="lua">
msg.Text = "This is the sample message text."</pre>
message.Text = "This is the sample message text."
message.Parent = workspace
</syntaxhighlight>
We can also, make it more simple like this:
We can also, make it more simple like this:
<pre> msg = Instance.new("Message", Workspace)
<syntaxhighlight lang="lua" line>
msg.Text = "This is the sample message text."</pre>
local message = Instance.new("Message")
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:
message.Text = "This is the sample message text."
<pre>wait(3)
message.Parent = workspace
msg:Destroy()</pre>
</syntaxhighlight>
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:
<syntaxhighlight lang="lua">
wait(3)
 
message:Destroy()
</syntaxhighlight>
So, now the whole thing:
So, now the whole thing:
<pre>function onTouched(hit)
<syntaxhighlight lang="lua" line>
msg = Instance.new("Message", Workspace)
local function onTouched(otherPart)
msg.Text = "This is the sample message text."
local message = Instance.new("Message")
message.Text = "This is the sample message text."
    message.Parent = workspace
 
wait(3)
wait(3)
msg:Destroy()
 
message:Destroy()
end
end


script.Parent.Touched:connect(onTouched)</pre>
script.Parent.Touched:connect(onTouched)
</syntaxhighlight>
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 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:
If you use debounce, it will look like this:
<pre>debounce = false
<syntaxhighlight lang="lua" line>
function onTouched(hit)
local debounce = false
if debounce == false then
 
local function onTouched(otherPart)
if not debounce then
debounce = true
debounce = true
msg = Instance.new("Message", Workspace)
 
msg.Text = "This is the sample message text."
local message = Instance.new("Message")
message.Text = "This is the sample message text."
        message.Parent = workspace
 
wait(3)
wait(3)
msg:Destroy()
 
message:Destroy()
 
debounce = false
debounce = false
end
end
end
end


script.Parent.Touched:connect(onTouched)</pre>
script.Parent.Touched:connect(onTouched)
</syntaxhighlight>


== Loops ==
== 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.
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
# While true do
#For loop
# For loop
#Repeat 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:
<syntaxhighlight lang="lua" line>
while true do
    wait(1) -- change this time to however many seconds you DON'T want there to be a message


===While true do===
    local message = Instance.new("Message")
    message.Text = "Please send me a friend request!"
    message.Parent = workspace


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:
    wait(300) -- change this time to however many seconds you want the message to remain


<pre>while true do
    message:Destroy()
wait(001) -- change this time to however many seconds you DON'T want there to be a message
end
msg = Instance.new("Message")
</syntaxhighlight>
msg.Parent = game.Workspace
=== For loop ===
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</pre>
 
===For loop===
"for" is used to make something go a set number of times. Unlike while true do the makes it go constantly.
"for" is used to make something go a set number of times. Unlike while true do the makes it go constantly.
For Example:
For Example:
<syntaxhighlight lang="lua" line>
local T = 5 -- How many times it does it.


<pre>
for i = 1, T, 1 do
T = 5 --How many times it does it.
    local part = Instance.new("Part")
 
    part.Parent = workspace
for i = 1, T do
b = Instance.new("Part")
b.Parent = game.Workspace
end
end
</pre>
</syntaxhighlight>
The 'for' sets i initially to 1 then adds 1 to i after each iteration has passed until i is equal to T.
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.
This would insert a part into the work space 5 times.
====Getting Children====
==== Getting Children ====
It is possible to use "for" to get to children of anything, e.g.,:
It is possible to use "for" to get to children of anything, e.g.,:
   
   
<pre>
<syntaxhighlight lang="lua" line>
a = game.Workspace:GetChildren()
local children = workspace:GetChildren()
for i = 1, #a do
 
if (a[i].className == "Part") then
for i, v in ipairs(children) do
a[i].Reflectance = 1
    if v.ClassName == "Part" then
else return end
        v.Reflectance = 1
    end
end
end
</pre>
</syntaxhighlight>
 
=== Repeat loops ===
===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.
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:
For Example:
<pre>
<syntaxhighlight lang="lua" line>
local numberValue = Instance.new("NumberValue")
 
repeat
repeat
Nv = Instance.new("NumberValue")
    numberValue.Value = numberValue.Value + 1
Nv.value = Nv.value + 1
until (numberValue.Value == 8)
until (Nv.value == 8) then
 
print("It stops at 8")
print("It stops at 8")
end
</syntaxhighlight>
</pre>
 
[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 13:31, 18 April 2023

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:

  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:

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.

  1. While true do
  2. For loop
  3. 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")