Explaining Scripts

From Goodblox Wiki
Revision as of 16:42, 22 June 2020 by Pizzaboxer (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Introduction

This article will explain each line of some big scripts and basic commands, from basic to advanced scripting. Before you read this tutorial, I recommend you see this first:

  1. Absolute beginner's guide to scripting
  2. Basic Scripting

Note: This is only useful in GoodBlox Studio.

Basic Commands

First of all, let me tell you about the very basic commands. Put this script into a part and we'll take it from there.:

function onTouched(hit)
print("Hi Mom!")
end
script.Parent.Touched:connect(onTouched)

Lets analyze it:

The first line:

function onTouched(hit)

Thist declares a function, a utility to the script. This utility is "onTouched", meaning "when touched". Then it says "(hit)". "Hit" is another variable that represents the thing that touched it. So when you touch the brick, the following will occur:

print("Hi Mom!")

This will print the simple phrase "Hi Mom!" in the Output Toolbar once the part has been touched.

end

This ends the onTouched function.

script.Parent.Touched:connect(onTouched)

First, "script.Parent" means the location in the GoodBlox Studio of the script, which will be the brick into which this script is inserted. Then we have Touched:connect(onTouched) that connects the touched function with the brick touched.

If Commands

Creates a condition to the script. Something will only be possible if the condition is real. Lets see:

i = 1
if (i == 1) then
print ("Hello! Thats correct!")
end

First we declare a value (i) and we say its value is 1. If i value = 1, then we print "Hello..." message! Of course, we have another commands to be used in if. Examples:

if (i.Parent == game.Workspace) then

This will only work if i's parent is game.Workspace.

if (i.Parent == nil)

Nil means non-existent, so this will only run if i doesn't exist or it is inside another parent.

  1. == - equals, the function will happen if true (i==1)
  2. ~= - different, the function will happen if false (i~=2)
  3. < - smaller, applicable only to values(i<5)
  4. <= - smaller or equal, will happen if i equals 5 or less(i<=5)
  5. > - bigger.
  6. >= - bigger or equal.

Also we can use equality of variables or non-existence.

if (i.Parent == game.Workspace) then

This will only work if i's parent is game.Workspace.

if (i.Parent == nil)

Nil means non-existent, so this will only run if it doesn't exist or it is inside another parent.

Loops

A loop is something that goes on and on until something declare is false or wrong. We have three types of loops.

  1. For i = 1
  2. While true do
  3. Repeat

"For i = 1" is used as a condition: if i is 1 than it will repeat until i is not 1. We can also use a marked number of loops.

A = 3
for i = 1, A do
print("Hello,World!") 
end

This script would do A times the loop, and since A is 3, it would print 3 times "Hello,World!".

A while true do loop will repeat and repeat and repeat until forever.

while true do
wait(2)
print("WOOOOOT")
end

The wait line makes it wait 2 seconds before printing. It will repeat the "WOOOOOT" message each 2 seconds. You can change it if you like.

A Repeat loop does the same as a for loop, only its simplier and it will repeat until a condition.

i = 0
repeat
i = i + 1
print(i)
until (i == 5)
print("No more loop!")
wait(3)

This is print "No more loop" only when i value is at 5. Dont mind at Instance. We will come to it later. Each 3 seconds the script checks if i value is 5. If it is, we have a print. If not, it loops until i is 5.

Instance

Is a command used to create things. You can create a brick from scratch to a explosion! See:

function onTouched(hit)
local brick = Instance.new("Part")
brick.Parent = game.Workspace
brick.Name = "Brick"
end
script.Parent.Touched:connect(onTouched)

If you insert this code in a new script and run it, when something touches it will create infinite bricks while you are standing on it because we don't have a if system and it could lag your computer and even bug your program!

Continue... "local brick = Instance.new("Part")" line makes a local variable (only works in the creation script) called brick and defines it as a new Part. We call its parent in game.Workspace, the normal address, and we define its name as "Brick"!

local e = Instance.new("Explosion")
e.Parent = game.Workspace

This script creates a explosion called as e variable and defines its adress in game.Workspace. (BOOM!) To define its values its another history.

Locals and Globals

See also: [1]

A local variable is declared using the local keyword. A local variable is only valid within it's scope , or enclosing block. For scope purposes, a script is considered an enclosing block. For example:

local foobar = "Goodbye World"

function foo()
	local bar = "Hello World"
	baz = "Global variable" --Note the absence of a local modifier
	print(bar)
end

foo() --Prints "Hello World"
print(bar) --Bar is out of scope, so prints nil
print(baz) --Baz is global, and therefore always in scope, so prints "Global variable"
print(foobar) --FooBar is in scope, so prints "Goodbye World"

Global variables, once declared, can be accessed anywhere (although current restrictions limit them to the scope of the script).

Vector3

Vector3 is a code made by three values (x,y,z) used in sizes definitions to positions. Y is height and x and z is sideways.

e = Instance.new("Part")
e.Parent = game.Workspace
e.Name = "Brick"
e.Size = Vector3.new(2,1,2)
e.Position = Vector3.new(10,10,10)

This is create a brick called "Brick" with a size of 2x2 and tall 1, or in the correct way a 2x1x2 brick. And its position is 10,10,10 used as coordinates in the game.

Humanoids

It means the human part of a brick. Like a player or a monster. If both have healths, can get damage and can walk, jump, and others, they are humanoids. We can use many functions and commands to humanoids, like loose life, increase max health, walk, and conditions too!

function onTouched(hit)
local human = hit.Parent:findFirstChild("Humanoid") 
if (human == nil) then return end 
print("*Hit by a human*")
end 
script.Parent.Touched:connect(onTouched)

FindFirstChild is only a search for the Parent that the script does and it uses the first thing named found!

local human = hit.Parent:findFirstChild("Humanoid")

This makes a local called human and it says that human is the first thing found called Humanoid (meaning, a human)in the hit's parent. In the fourth line we make a condition:

if (human == nil) then return end

If human is unexistant then returns and it ends. Meaning: if a humanoid didn't touch that, it won't do anything. Otherwise, if a human touched that(human~=nil)...

print("*Hit by a human*")

Prints "*Hit by a human*". This condition is very useful for buttons and other things you don't want to be active with only a rocket or a bullet.

Now, health.How to make lava is a good example of a health condition. If a human touches it. Its life is 0. We can also increase the life of a human.

function onTouched(hit)
local human = hit.Parent:findFirstChild("Humanoid") 
if (human == nil) then return end 
human.MaxHealth = 500
human.Health = human.MaxHealth
end 
script.Parent.Touched:connect(onTouched)

This makes the human's maximum health increase to 500, and then makes the human's current health equal to that maximum health (which is 500). Using MoveTo, we can order a humanoid to walk somewhere. You will need to create a brick that will trigger the following script.

function onTouched(hit)
local human = hit.Parent:findFirstChild("Humanoid") 
if (human == nil) then return end 
base = game.Workspace.base
human:MoveTo(Vector3.new(10,10,10), base)
end 
script.Parent.Touched:connect(onTouched)

This will make the human walk to coordinates 10,10,10.

See Also