Fandom

ROBLOX Wikia

Beginners Guide to Scripting

12,564pages on
this wiki
Add New Page
Talk0 Share
This page is under construction
This tutorial is easy.

Introduction

Hi there, this is a scripting tutorial designed for absolute beginners to scripting.

Please note that this page is currently under construction, and it is incomplete and will likely have some spelling or programming errors. If you notice one, please feel free to fix it.

For the more advanced scripting tutorial, please click here. If you are looking for a list of Tutorials or an advanced guide that will explain everything else, please click the below link.

You can also click here for a list of all tutorials and guides
Authors of this section include:

What is Scripting?

(By User:Robotcoder)

Scripting, also referred to as Programming, is the act of using an programming language to program or "tell" an application what to do. ROBLOX uses a programming language called Lua. It is similar to JavaScript, but is less powerful in certain ways, and much easier to use, hence ROBLOX choosing it over JavaScript.

Throughout this tutorial you will not only learn how to Script / Program in ROBLOX, but also learn how it works.

Setup

Start by opening up Roblox Studio if you haven't already, and make sure the Output (View>Output), the Explorer (View>Explorer), the Properties (View>Properties), and the Command Bar (View>Toolbars>Command) are all opened. (They should be by default).

NOTE: It is easier to Script with the Run without character option "Run" instead of "Play". To switch, click the down arrow underneath the "Play" button, and select the Green arrow, as opposed to the Blue arrow with a ROBLOX avatar.

Creating a script

This is relatively simple. Make a new place or edit one with Studio, for this example lets create a place called "testplace". Next lets right click in the Explorer box (see #1) Workspace>Insert Object and select Script, NOT to be confused with a Local Script (will be explained later). This should put the script within Workspace. Double click on it to view its source (the code to be executed). The following should appear:

​print 'Hello World!'

What does this do?

Pps

Play with character, pause, and stop

Go ahead and click on the play button, and you should see "Hello World!" appear at the in the Console / Output log. How does this work, then? "print" is the function that is called. This will be explained later. 'Hello World!' is the parameter, which is basically a manipulable value that is given to the function. This will be explained in a second.

Variables

A variable (if you've had Algebra yet you'll recognize this term) is a non-constant (it can change) object. This is best shown by example:

a = 5

b = "Hello World!"

c = a

print(a)

print(b)

print(c)

This should result in:

5
Hello World!
5

As you can see, the variable c returned 5. Why is this?

Well, in Lua, to output a string, which is a fancy way of saying a group of letters, numbers, and symbols, you must put the string inside either double quotes (") or single quotes(').

Example:

abc = "hello'

Will not work, because you must open and close with the same type of quote, but the below will

abc = "hello"

But why did c return 5? Well if you remember we said

c = a;

But remember that a is a variable name, and a actually equals 5. Also, another thing. It is more common and often more practical to pass in parameters like so:

FUNCTION_NAME(PARAMETERS_IF_ANY)

Now, how did our script print a, b, and c? When it printed out a it found the value of the variable a, which is 5, and output it. Same with b, and with c, it found its value, a, then found a's value, 5. This is useful in scripting in almost all cases. Also, remember that parameter for print? That is actually a variable!

Removing a characters head

This is a simple task. Type this into a script.:

game.Workspace.Player.Head:remove() --DAI

DO NOT click the run button this time. Instead, go Home > Test > Play.(The icon should be a blue arrow with a ROBLOX avatar on top.) You should spawn in as usual, except your name will be "Player", your chat bar will be missing, and you'll have access to Studio.

When you spawn in, your head should be removed. How did this happen? Let's examine it again.

"game", or DataModel as it is referred to sometimes, is the at the very top of the "Hierarchy" (The Explorer box lists all items in the Hierarchy) of ROBLOX, which essentially represents the entire game file that you currently have open. The period symbolises going down the hierarchy tree. Next in the hierarchy tree is the Workspace, which contains all the blocks you have in your game. Player is your character (which would be someone's name in the normal game), Head is your, well, head, and :remove() deletes it. DO NOT delete the entire "Player", it won't respawn automatically.

Also, "--" means a comment. Comments are ignored by Lua completely, and are useful for documenting code. Be aware, "--" will only 'comment out' one line of code. To comment multiple lines of code out, type

--[[

to start a multiline comment. Also be sure to type

]]--

At the end to end it.

Example:

--[[

This code will not 
run

]]--

What if [INSERT OBJECT HERE] doesn't exist?

Simple. Use an "if" block. Here's how:

character = game.Workspace:findFirstChild("yomonalol") --Find him..

if character ~= nil then --Does he exist?

character.Head:remove() --If so, kill him. >:D

end

There's a lot of new terms in there. Let's break it down, as usual. ":findFirstChild("yomoalol")" attempts to find yomonalol, and if he's not there, sets the variable "character" to nil. Otherwise, it sets it to what it finds. What is nil, you ask? Nil is Lua's representation of nothing. Now, why can't we just say the following..?

character = game.Workspace.yomonalol

Because, if he isn't there, the script will not work and stop running anything after that line.

if character ~= nil then

This means is the character not nil. '~=' means not equal to in Lua. '==' means equal to in Lua. What is the difference between '==' and '='? A single equal is for assigning variables, a double equals is for checking if they are equal to something.

Killing him should be self explanitory.

The "end" is where a lot of people getting really confused. Think of it like this:

if [something] == [something_else] { --Replace then with curly bracket..
--do stuff
} --Replace end too.

If something needs a "{" then it also requires a "}"

Changing a Property of an Object via Script

(By Omnisxiii2)

Say you want your script to do something more than break things or print out endless values of what you get when you remove the cheese and tomato sauce from pizza. You want to make the time of day suddenly change in 10 seconds to night. So...

What I would recommend as your first step is to put a script in the item that you want to modify. In this case, I'm going to put a part inside the object "Lighting" because that's where the TimeOfDay property is. Placing it anywhere will work too, but you will need to do some extra work.
Now, you will begin scripting. You (by default) start with this:

print "hello world!" 


Clear that junk out. You're a big boy now. If else, read the stuff above this to get a grip of everything.
Now, the script is UNDER/INSIDE the LIGHTING item in your game. It is listed under LIGHTING. We call this a "child" of LIGHTING. In this case, LIGHTING would also be the "parent" of our script. Think about it. The parent is the guardian of a child. The child goes with its parent.
Anyway, since we know that our script is a "child" of LIGHTING, we can be a little lax about our work. See below:

script.Parent 

This tells the script to find the "parent" of the script. TYPE "PARENT" WITH A CAPITAL P. Lua gets fussy sometimes and we need to serve it everything the way it wants it.
With that out of our way, we can now tell the script what to do. Let's tell it what property it needs to change. Go to the properties window, and then in the explorer, select "Lighting". Find out what is the name of the property that we want to change. I did your homework for you, it's "TimeOfDay".
Now that we have the property, place it right after the item we want to change, separated by a period. For instance...

script.Parent.TimeOfDay 


Now we tell it what to set the property as. In this case it is a number. We want nighttime, so...

script.Parent.TimeOfDay = (0) 


It is important to put a number inside the parentheses. If you have text instead, make sure you use quotation marks, like...

script.Parent.Text = "Hello!" 


Good work.

Without Placing the Script as a Child

What if you want to change the TimeOfDay property of Lighting without putting the script in Lighting? There's another way to do the same as the above.

Game.Lighting.TimeOfDay = (0) 


"Game" refers to your entire game. Remember that. In order to refer to something in your game via script, you have to first say that it's in the game, then which major category it's in (like Workspace, StarterGUI, etc.), then parent after parent until you reach the item, then what will happen to it.

The only built-in shortcut in this system is "Workspace" instead of "Game.Workspace".

Adding Notes to Your Script

(By Omnisxiii2)

This technique is mentioned earlier in this guide, but because it wasn't mentioned that well, I'll do a quick touch-up here. Say you have some crazy function...that turns out to not work. Here, I'm going to make a really dumb script that does not work...

script.Parent = "Dumb" 
AGH DUMB DUMB SCRIPT.Workspace.R700:Destroy() ugafluf 
Game.Lighting.TimeOfDay = (100000000000000000000000000000000000.00000000001) 
YAY Skippee
if else then 
          end 
end
end
print "My Granny Is A Superwoman" 

Obviously, this does not work. You want to fix it so it does work (or else you wouldn't be trying to learn this stuff). But you're going away to North Dakota for 16 millennia. There's no internet or power there, and you bump your head there. In which case...a note works perfectly to remember everything. Just type -- to a point in the script and then your stuff. So...

script.Parent = "Dumb" 
AGH DUMB DUMB SCRIPT.Workspace.R700:Destroy() ugafluf 
Game.Lighting.TimeOfDay = (100000000000000000000000000000000000.00000000001) 
YAY Skippee        --This doesn't work, remember to fix it.
if else then 
          end 
end
end
print "My Granny Is A Superwoman" 

That will work perfectly. If run, the script will also ignore whatever is in your note, making it a good technique in making scripts for others to configure.

That's my lecture. Have a good day.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.