From Goodblox Wiki
Jump to navigationJump to search


Functions perform specific tasks. Some functions are already predefined, and some functions you create yourself. The idea behind the functions is the same.

Simple functions

Let's take a look at a simple function.

local function myfunction() -- what this function does
     local x = 2+2

myfunction() -- call the function 

Everything between function and end simply describes what your function will do. Here, you are

  • Allocating a value of 2+2 to x
  • Printing x

It's important to note that your function won't run, i.e., you won't see the value of "4" appear on your screen unless you specifically call it. That is what the last line is, namely:


If you delete this line, nothing will get printed to your screen.

Creating new bricks

You can make a function do all kinds of things. For example, the following function will create a new brick:

local function newbrick()
     local p ="Part")
     p.Parent = workspace
     p.Name = "Brick"
     p.Anchored = true


Here, the function (called "newbrick()") is creating a new Instance of a brick. That brick will be in the Workspace. We are inserting a specific object, namely a Part, and naming it "Brick". We want to Anchor it so the brick won't fall down.

Lastly, we call the function so that the function will execute.

Action-triggered functions

Functions don't have to be called by writing out a command. They can be called by a human action, such as clicking a mouse button.

  • Insert > Object > Part
  • Anchor that newly inserted brick.
  • Click that brick.
  • Insert > Object > ClickDetector. This will allow the brick to detect when it is being 'clicked'.
  • Insert > Object > Script.
  • Double-click that script file, and insert the following:
local function clickabrick()
     print("Hi mom!")

This function, called "clickabrick()", has to be told what to do. The function will print "Hi mom!", but only if the function is called, namely, by clicking that specific brick.

The last line is more complex now, but it has the same purpose as before -- calling the function. On the left of the colon (:), you are describing the relation of where the triggering action (i.e., a MouseClick) is to the function:

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

See Also