Scripting 1: Indexing

How to index objects

by kitrank

Author Avatar

Introduction

This is the first tutorial of a series (in the making) covering how to index or reference objects by script. This tutorial will cover part of the basics and assume the reader is completely new to scripting or programming in general.

Setup

The first thing you need to do in preperation is setup your required windows. The Explorer, Output, and Properties windows are essential. • The Explorer window is opened by default and you are probably familiar with it already, but if you aren't, the Explorer window is used to view the heiarchy of objects in the place. • The Output window is basically every scripter's best friend. It is used to help debug (fix errors) and test scripts after they run. If something goes wrong with a script, it will usually output the problem to the Output window. It is able to tell you which line of code in the script is malfunctioning and how it may be malfunctioning. • The Properties window is used to view and manually change the properties of the object currently selected in the Explorer window. You do not absolutely require this window, however it is useful for browsing what properties a certain object has so that you know how to manipulate them by script. How to enable these windows: Anywhere in the empty space of the top bar (shown below), right click, and then left click on the respective windows.

img|8000x730

Inserting objects

In the next section we will be indexing a Part as our first example, but first we need to insert one. To do so, hover over the Workspace in the Explorer window and click the plus icon, then select Part. We also need to insert a script to be written in. Hover over ServerScriptService in the Explorer window and click the plus icon, then select Script. Do not select Local Script or Module Script since these are not the same type of script and work differently. ServerScriptService is usually the most logical place to store (regular) scripts, however they can also work under anywhere in the Workspace.

Indexing

Every script will start with this single line in it:

print("Hello world!")

You can just delete this line since all it does is print text in the Output window. Now we can index the Part we created in the Workspace like so:

game.Workspace.Part

What we just did is reference/locate/name the Part, which is Indexing. This code by itself would be incomplete an woulddn't do anything because we are just naming it so that we can do something to it next (in the next tutorial, properties). We did this by going down the family tree or heiarchy of objects.

img|840x500

• Part is inside Workspace and Workspace is inside game, although you cannot see it in the Explorer window. That is why you start with game. Instead of game.Workspace, you can do this instead:

workspace.Part

• workspace is short for game.Workspace. Now we will do something a little more complicated, indexing an object with a space in its name. Insert another Part in the Workspace but move it inside the first Part in Workspace, and manually name it to Secound Part. To name an object, select it in the Explorer window then view the Properities window, scroll down to Name and change the value. Your Explorer window should now look like this:

img|855x600

To index Second Part which has a space in its space, we do it this:

workspace.Part["Second Part"]

As shown above, Second Part is inside Part which is inside Workspace which is inside game, which we cannot actually see in the Explorer, and we are using a shortcut to reference the Workspace instead of like this:

game.Workspace.Part["Second Part"]

Conclusion

Hopefully these two examples were enough to help you understand how index works, since you are just going down the heiarchy or objects inside each other. If this tutorial was helpful, please give a rating for more possible tutorials in the future. Thanks for reading (if you made it all the way through)!

View in-game to comment, award, and more!