In-Depth Scripting Guide

From Goodblox Wiki
Revision as of 17:28, 22 June 2020 by Pizzaboxer (talk | contribs) (Created page with "{{CatUp|Lua Help}} ==What You Need!== *'''GoodBlox Studio''' *'''Explorer Tab''' - This tells you exactly where everything is. *'''Properties Tab''' - Thi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


What You Need!

  • GoodBlox Studio
  • Explorer Tab - This tells you exactly where everything is.
  • Properties Tab - This tells you all the things editable in an object.
  • Output - SCRIPTER'S BEST FRIEND. It tells you any mistakes (syntax, values, ect) found in a script, and where to find it.
  • Command Prompt (Optional) - Not really needed, but is a great way to test out one-lined wonders.
  • Any script; new or old (Optional) - If you want an example script to look at, you can always pick one out of the toolbox.

Order of Operations; Script Style (VFC)

There is an order of which you should add your operations, just like in math. A mnemonic device for this order is "VFC", for "Variables, Functions, and Connections". A good script usually has itself in a neat and tidy order so that almost anyone can understand what the script does just by looking at it.

Variables

Usually in a script, there is a specific order in which you put your operations. First off, one of the most important things to put in the very beginning are variables. Variables are used to note objects that will be used a lot. If you like to be organized, you like to put things in models. Sometimes, that model chain can get pretty lengthy.

Example:

example = game.Workspace.Example

Notice how the variable has a name, which is "example." The name can be anything except numbers alone. If you have a name of just numbers and you use it in a script, you will end up with a syntax error. When defining a variable, you need to tell exactly where it is. The Explorer tab helps with this as there is always a little tab telling you that that object is a child of something. When typing the parental of the object you want, spell the parental(s) correctly. You do not need the function ":FindFirstChild()" when making variables.

Functions

Secondly, in many scripts, you will come across a function. A function is something that can be used repeatedly. Some functions are also used to shorten script length, and some are just there because someone didn't want to delete it. When I create functions, I create the ones without a connection first, and then I create the ones with a connection line afterwards. If I do it like this, I can use the non-connections in the ones with connections. After all the things inside, you always close a function with an "end" statement.

Example:

bin = script.Parent

function anchor(object)
object.Anchored = true
end

function onTouched(part)
anchor(part)
end

bin.Touched:connect(onTouched)

There is the one variable, not really needed but I need to stress its importance. In the first function, as in every function, the term "function" is first. It is correct if the term turns purple. After the term "function," the function needs to be named (in the example, there are two -- named "anchor" and "onTouched".) Do not use numbers as a name. It is better to spell out numbers in names, except in some occasions. Now, after the name, you'll see the word "object" in parentheses. You can rename it anything you want, except numbers, and you can use it for anything you want. It's like a function variable, you can use it to do other things and use it repeatedly with different terms. You can also add more variables to this by using commas. So, this example could have been made: "function anchor(object, statement)". In this function, though, it is anchoring the object no matter what. Finally, the "end" statement.

In the second function, the function has a connection line. It's the same as the first function, the "function" statement, the function name, and the variable. The difference is, the previous function anchors the so-called "part." The variable is defined as "part," which is in this case, the object being touched. Finally, the "end" statement as always.

Connection Lines

it If you look in the above example, you will notice a connection line. A connection line does what it says, connects the function to something done. The layout for a connection line is very simple. This is also one of the parts of a script when the scripter is most happy, before they test it out, that is. The connection lines usually tell you that the script is done and it is time to test.

Example:

bin = script.Parent

function anchor(object)
object.Anchored = true
end

function onTouched(part)
anchor(part)
end

bin.Touched:connect(onTouched)

Presuming this script is inside of a brick, it can therefore be touched. In the variables, the brick is called "bin." In the connection line, there is a specific order in which the line should be. It should be like this:

(Object).(Action):connect((Function Name))

The object "bin," is therefore being "Touched" and we are connecting it being touched with the function "onTouched."

NOTE: To find actions, check in the Object Browser. To get to the Object Browser, go into GoodBlox Studio, press "Help" on top, then hit Object Browser. When there, you can find what you can do to objects by clicking on their name and scrolling all the way down. The actions are the ones with a lightning bolt next to them.

Basic Lua Terms

Throughout Lua, you will find terms like 'if', 'local', and 'for'. In this section, I will teach you Basic Lua terms. I recommend, if you haven't, to read the Order of Operations section of this guide before going on with this one.

The 'if' Statement

"An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional."[1]

Example One

brick = game.Workspace.Brick
if brick~=nil then
print("The brick is here!")
end

First is the "brick" variable, followed by the "if" statement. The "if" statement is asking whether the "brick" is not "nil". "Nil" meaning nothing. This means that if the brick is there, it will print in the output, "The brick is here!" On the next line, there's the 'end'.

A common mistake is if you are makeing the 'if' statement check a variable, be sure to put == not =. == means equal to. = means __ is __

Example Two

number = 0
while number < 10 do
wait(1)
number = number +1
if number > 5 then
print("Number Greater Than 5!")
end
end

On the first line, "number" is 0. The next step is a 'while true do' loop. It waits one second then "number" will increase by one. Then there is the 'if' statement. It is now asking if "number" is greater than 5. If "number" is greater than 5, it will print, "Number Greater Than 5!" If it isn't greater than 5, then it will just skip it and repeat the loop. Once "number" turns to 11, the loop will end. This also goes with connection functions.

TIP: When using 'if' statements, use the proper Operations of comparison.

The 'else' Statement

Say you have an 'if' function, but it doesn't meet the requirements. That's when the 'else' kicks in. It does the function of the else onwards.


Example:
if game.Workspace == game.Lighting then
print("NOWAI!!!")
else
print("Whew. That was close.")
end

Well, there's the 'if' statement, which is impossible. Now, if it can't fulfill the requirement, it goes to the 'else' statement. So it does its function and prints to the output the statement in quotes. Then the end.

The 'elseif' Statement

'Elseif' is not an 'if' function. So what is the difference between 'elseif' and 'else if'? 'Elseif' is counted as an 'else' with a requirement for it to be run, and doesn't need an end. On the other hand, 'else if' is comprised of two statements: an else, and an if. Therefore, it needs a 'end' for the 'if' statement. 'Elseif' is ALOT more secure that 'else', because the lines of script after it only runs if the condition after the 'elseif' is met. Unless you want the folowing lines to be run only if the 'if' statement is false, then use 'elseif' to make sure that the lines only run if the 'elseif' condition is met.

waffleboy = true

Example 1:
if waffleboy == false then
print("NOWAI!!")
elseif waffleboy == true then
print("YAWAI!!")
end

Example 2A:
if waffleboy == false then
print("NOWAI!!")
else if waffleboy == true then
print("YAWAI!!")
end
end

Example 2B:
if waffleboy == false then
print("NOWAI!!")
else
if waffleboy == true then
print("YAWAI!!")
end
end

'Elseif' doesn't need an end, but 'else if' does, because Example 2A and Example 2B are the same.

The 'repeat' Statement

This will keep on repeating what is put after until the 'until' statement is achieved.

a = 0

repeat
a = a + 0.1
until (a = 1) then
print("a equals to 1!")
end

The 'until' Statement

The until statement always goes after a repeat statement. Whatever is inbetween 'repeat' and 'until' will be repeated until the statement in between the parentheses is achieved.

a = 0

repeat
a = a + 1
until (a = 8) then
print("a equals 8")
end

The 'end' Statement

Always remember to put this to end an 'if' statement. This can also end the 'while' functions and 'for' functions and functions themselves. Also, either 'repeat' or 'until' statement requires an end. EIther way, you will still need one. Example for 'if':


value = true

if value == true then
print ("You are correct!")
end

Example for 'for':


a = game.Workspace:GetChildren()
for i = 1, #a do
if a[i].className == "Part" and a[i].Locked == false then
a[i]:Remove()
end

Example for 'while':

a = 0
while true do 
if a == 1 then break end
a = a + 0.1
wait(1)
end
end

Example for function:

function clean(part)
part:Remove()
end

clean(game.Workspace.Example)

The 'local' Statement

Whenever you are scripting and you canot make something a variable, like making a variable out of a function variable, you should use the term 'local'.

Example:

bin = script.Parent

function onTouched(part)
local h = part.Parent:FindFirstChild("Humanoid")
if h~=nil then
print("Hi!")
end
end

bin.Touched:connect(onTouched)

'local' is just what it is, local. It is not a global statement like variables are, so it can be used in a whole different function, as long as it isn't stated already in the same function. Remember if you (for example) define a local variable for a global variable, and you edit the local variable, the global variable will not change, unless you put the local = global

local a = game.Workspace:findFirstChild("Score").Value
while true do
a = a + 1
game.Workspace:findFirstChild("Score").Value = a -- This line basically "Updates" the real varible value with the "a" value.
wait(1)
end

The 'while' Condition

While is a condition. If it is false, the loop will end. If it is true, the loop will be executed, and the process will be repeated.[2]

Example one:

while true do
wait(3)
print("Hello World!")
end

Example two:

example = true
while example == true do
wait(3)
print("Hello World!")
example = false
end

Example one shows a function that will never end. It will wait 3 seconds, then post into the output 'Hello World!'. In example two, it does the same thing except this time, 'example' must be true for it to run. This one will wait 3 seconds, print the same thing, then turn its example to false. If you tried it, you will notice that it only runs once. Remember, all function statements need an 'end'. Note: Whenever a "while true do" is running, you always need a "wait()" statement. If you do not have a "wait()" statement, your place will not function and it will just hang, and lock up your program.

The 'for' Statement

This goes through all of the objects noted and chooses different objects based on your 'if' statements.

Example One:

for i=1, 6 do
print("Hi")
end

This repeats the function 6 times, each time printing the word "Hi".

Example Two:

bricks = 0
local c = game.Workspace:GetChildren()
for i=1, #c do
if c[i].className == "Part" then
bricks = bricks + 1
end
end
print(bricks)

The first line tells you that there are 0 bricks. Now we are naming our parent, "c", a very common term when using 'for'. Now the 'for' statement. "for i=1, #c do" says "go through the all of 'c'" Now the 'if' statement. When noting each brick in a 'for' 'if' statement, you need to have it in this format: "c[i]" (Based on this example) The script goes through everything in the Workspace, and if the object is a Part, the brick count goes up by one, then prints into the output the number it ends up on.


See also


Built-in functions
Programming in Lua