Scripting Book

From Legacy Roblox Wiki
Revision as of 11:39, 13 July 2011 by >SoulStealer9875
Jump to navigationJump to search
Warm Welcome
Welcome to the scripting book written by SoulStealer9875! I hope you learn alot here as I try my best to teach you chapter by chapter.


This is still being created, I do not wish for you to edit this book until it is complete.

Introduction

What is the Scripting Book?

The Scripting Book was created by SoulStealer9875 as an inspiration from Camoy's Cookbook. This book is incomplete and when it is completed it will give you a complete runthrough of how to script. From the basics to the advanced.

Basics

Output

Output is a window that displays bugs in your broken code. It also displays text that you have Printed. To view the output window, open up Studio and go to View -> Output.

Printing

In scripts, to print doesn't mean to print a document onto paper - it means to print text into the output window. Insert a script into workspace by opening ROBLOX studio and going to Insert -> Object -> Script -> OK.

Open up Explorer, press the '+' sign next to Workspace and find 'Script'. Double click on 'Script' and you will see there is already a line of code written for you. It should look something like this:

print 'Hello World'

Open up output and press 'Run'. Then the output window will have text in it. It should say

Running Script: Script
Hello World.


That is because in the script's source, it says Hello World in the two quotation marks. If you changed that to 'blah'

print 'blah'

And then press Run, the output window will say

Running Script: Script
blah


Let's Begin

These are the basics of scripting.

Your first script

ROBLOX's scripting language is their version of Lua. We call it RBX.lua (Short for ROBLOX Lua). In ROBLOX Studio, when you open up explorer (View -> Explorer) you see many different folders. It should look something like this:

If we wanted to locate a descendant of 'Workspace', we would write a path to it. All the services (folders you see in explorer) are descendants of 'game'. There are three ways of accessing workspace by using a script, these are

game.Workspace
Workspace
workspace

Let's say we have a WedgePart in Workspace and we wanted to delete it. We would locate the WedgePart and use the :Remove() function to delete it.

game.Workspace.WedgePart:Remove()

Does exactly what it says on the tin. It locates WedgePart, and it removes WedgePart, setting it's parent to nil.

Variables

Variables can be used to index a path. I'm going to create a variable that contains the path to 'WedgePart', and then use the variable name to remove WedgePart from Workspace.

e = game.Workspace.WedgePart
e:Remove()


Simple enough, whatever we do with 'e' would edit the WedgePart in workspace. If we didn't remove the WedgePart, we could do anything to it. We could have renamed it, recoloured it and the possibilities go on.


Mathematics in RBX.lua?

You can use a variable to hold mathematics, pretty much like algebra! Then you can print the variable into the output window and there's your math homework completed.

e = 45
b = e + 5
print(b)

Would print 50 in output, because b's value is e's value, which is 45, plus 5. So b's value is 5 more than e's value, making b's value 50. So when we print b's value in output, it will print 50.

Loops

What is a loop? A loop is something that does the same thing over an over, some loops go on forever, some loops stop when a condition returns differently.

There are three kinds of loops:

while CONDITION do

end
for i = NUMBER, INCREMENT do

end
repeat wait()

until CONDITION

The first example is ofternly used as an infinate loop as such shown below:

while true do
wait()

end

The second example repeats the same thing over and over for a given amount of time, example, if we wanted to print 'Hello World' in output 5 times, we would do this:

for i = 1, 5 do
print 'Hello World'
end

> Hello World
> Hello World
> Hello World
> Hello World
> Hello World

And the last example repeats the same thing over and over until a condition is changed. For example,

repeat wait()
game.Workspace.Part.Position = game.Workspace.Part.Position + Vector3.new(1, 0, 0)
until game.Workspace.Part.Position.X >= Vector3.new(10, 0, 0)

You can do the the second example and the third example as infinate loops by doing this:

for i = 1, math.huge do

end
repeat wait()

until false

Instance

Instance is a table that contains all the objects you can find when you open up ROBLOX Studio and go to Insert -> Object. Instance creates a object that you have selected from the object table and parents it in the given directory (or nil if not given a directory).

We want to create a variable. It can be named anything you want. I am calling my variable 'WP' - short for WedgePart, because I want to create a WedgePart.

WP = 


Next we need to use Instance, but, since we are creating an object from the table we need to put '.new' after Instance and an open parenthesis (braket).

WP = Instance.new(


Now we need to tell the script what object we wish to pull out of the object table. Which my choice was WedgePart, but we need to tell the script that this is a String so we're going to put speechmarks around WedgePart.

WP = Instance.new("WedgePart"


That's good so far, now we're going to parent it in Workspace. So we're going to put a comma after the string. The comma seperates the two arguments, then we're going to put game.Workspace and then a close braket.

WP = Instance.new("WedgePart", game.Workspace)


The example above creates a WedgePart and puts it in Workspace.

WedgePart's Properties

Every object has properties. Here's a list of a WedgePart's properties:


Accessing These Properties

To access these properties, we need to locate the WedgePart,

game.Workspace.WedgePart

Next we need to add a period. This period will tell the script that we're going to access one of it's descendents or properties. In this case, property.

BrickColor Property

First, we're going to edit the BrickColor property. So after the period we add BrickColor.

game.Workspace.WedgePart.BrickColor

Next, we're going to add an equals sign, to tell the script that we're going to edit that property. Then after the equals we need to add BrickColor.new to tell the script we're changing the value of that property and then an open braket.

game.Workspace.WedgePart.BrickColor = BrickColor.new(

There are many different colors to choose from. I am changing the WedgePart's color to Bright blue because I like that color. So we need to put speechmarks around the value to tell the script that we're going to insert a script, then a close braket to tell the script where the function stops.

game.Workspace.WedgePart.BrickColor = BrickColor.new("Bright blue")

Material Property

The material property contains 9 Enum values.

Plastic Wood Slate Concrete CorrodedMetal DiamondPlate Foil Grass Ice
0 1 2 3 4 5 6 7 8

They can be edited by using the Material property.

game.Workspace.WedgePart.Material = ENUM_VALUE

Where it says 'ENUM_VALUE' we can change that to one of the values above. For example, if ENUM_VALUE was 0, the material of WedgePart would be Plastic, if it was 6 the material would be Foil.

Reflectance

The reflectance property goes from 0 to 1. 0 being not shiny atall and 1 being extremely shiny. 0.5 being in the middle.

game.Workspace.WedgePart.Reflectance = 0.5

Transparency

The transparency property goes from 0 to 1. 0 being opaque and 1 being invisible. 0.5 being transparent.

game.Workspace.WedgePart.Transparency = 0.5

ClassName

The ClassName property can not be changed. This property determinds what this object is.

Name

The name property is set to the kind of object by default. If we change this property, whenever we next use a path to it, we have to use its new name. We need to put speechmarks around its value to tell the script that the name is a string

game.Workspace.WedgePart.Name = "Hai"


Parent

The parent property determinds which directory the object is a descendant of. If we changed it to something other than Workspace, the object will not be visible and all scripts descending from that object will be disabled.

game.Workspace.WedgePart.Parent = game.Workspace

Position

The position property contains three values, since the game is 3D (or 2.5D however you want to say it), the values are in axes, X axis, Y axis and Z axis.

To change this property we need to use Vector3.new and an open braket to tell the script we're changing the WedgePart's position.

game.Workspace.WedgePart.Position = Vector3.new(0, 0, 0)

The first 0 is the X axis, the second 0 is the Y axis and the last 0 is the Z axis.

Anchored

The anchored property determinds whether or not the object can fall. It's a boolean, meaning that it can only have two values, true or false. If it's value is true, the object cannot fall. If the value is false, the object can fall

game.Workspace.WedgePart.Anchored = true

Archivable

The archivable property is also a boolean. If it's value is true, the object will not be visible when the place is published and it cannot be cloned.

game.Workspace.WedgePart.Archivable = false

CanCollide

The cancollide property determinds whether or not you can walk through the object. This property is also a boolean. If it's value is true, you cannot walk through it. If it's false then you can walk through it.

game.Workspace.WedgePart.CanCollide = false

Locked

The locked property determinds whether or not you can move the object with the mouse. If it's value is true, you cannot drag the object with the mouse in/out of ROBLOX Studio, if it's value is false, you can drag the object with the mouse.

game.Workspace.WedgePart.Locked = true

Size

The size property is like the position property, only it resizes the object instead of re-positioning it the reason it is simillar to the position property is because it also uses the 3 axes, X,Y and Z. We also still use Vector3.new(x,y,z) to edit it's values.

game.Workspace.WedgePart.Size = Vector3.new(5, 5, 5)

formFactor

The formFactor property is one I always mispell as formFactory, so I have to be careful here. The formFactor property is used to change the form of the object.

This also uses Enum values.

Symmetric Brick Plate Custom
0 1 2 3
game.Workspace.WedgePart.formFactor = 3

Mathematical Signs

Table of mathematical signs that will help us later in the book:

To the power of Percent Divide Multiply Plus Subtact
^ % / * + -

Continue to Chapter 2