Basic Scripting: Difference between revisions
Pizzaboxer (talk | contribs) No edit summary |
Pizzaboxer (talk | contribs) No edit summary |
||
Line 9: | Line 9: | ||
== Requirements == | == Requirements == | ||
This tutorial is for people familiar with GoodBlox, and know [[GoodBlox_Studio|GoodBlox Studio]] very well (a few weeks of experience). They should be familiar with [[Image:Explorerwindow.JPG|Explorer]], [[Properties]], and [[Image:Outputwindow.JPG|Output]]. | This tutorial is for people familiar with GoodBlox, and know [[GoodBlox_Studio|GoodBlox Studio]] very well (a few weeks of experience). They should be familiar with [[Image::Explorerwindow.JPG|Explorer]], [[Properties]], and [[Image:Outputwindow.JPG|Output]]. | ||
== Important Things To Remember When Scripting == | == Important Things To Remember When Scripting == |
Revision as of 15:57, 22 June 2020
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 GoodBlox, and know GoodBlox Studio very well (a few weeks of experience). They should be familiar with [[Image::Explorerwindow.JPG|Explorer]], Properties, and .
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.
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 most scripts. This is the first major line you will use,
function onTouched(hit)
which is the function line for touch scripts.
Note again that the spacing and capitalization are very important. The next major line is the last one to most scripts:
script.Parent.Touched:connect(onTouched)
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.
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.
Another example
Before the function onTouched(hit) line, define the door with this line:
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)
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:
- IntValue
- Message
- 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:
- Parent
- Text
Explosion:
- Parent
- BlastRadius
- BlastPressure
- Position
IntValue:
- Parent
- Value
Lets 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:
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."
But we need to make it go away, after 3 seconds. The remove() function will remove the message from the screen. Remember to put a colon between msg and remove:
wait(3) msg:remove()
So, now the whole thing:
function onTouched(hit) msg = Instance.new("Message") msg.Parent = game.Workspace msg.Text = "This is the sample message text." wait(3) msg:remove() 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") msg.Parent = game.Workspace msg.Text = "This is the sample message text." wait(3) msg:remove() 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.
- While true do
- For 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:
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:remove() 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:
T = 5 --How many times it does it. for i = 1, T do b = Instance.new("Part") b.Parent = game.Workspace end
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.,:
a = game.Workspace:GetChildren() for i = 1, #a do if (a[i].className == "part") then a[i].Reflectance = 1 else return 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:
repeat Nv = Instance.new("NumberValue") Nv.value = Nv.value + 1 until (Nv.value == 8) then print("It stops at 8") end