From Goodblox Wiki
(Redirected from Events)
Jump to navigationJump to search

An Event is a special kind of GoodBlox Object. It provides a way for user-defined functions to be called when something happens in the Game.


Events have these functions:


The connect method establishes a function to be called whenever the event occurs. The function is called with the arguments for that event. The connect method returns another special object called a connection object.



This causes myTouchedFunction to be called whenever that Part is touched by another Part.


Works like the global wait function except that instead of waiting for a fixed period of time, it waits for the next occurrence of the Event. The return value is arguments of the event.



This causes the script to stop and wait for that Part to be touched by another Part.

deprecated: disconnect

The old method of using Event:disconnect method is superceded by Connection:disconnect.


Take the example of the Touched event of Part:

Syntax Touched( Instance part )
Description: Fired when another object comes in contact with this object.
In Object: BasePart

The first section tells you there's an event named "Touched" which has the argument "part". The middle section explains when the event will occur and why. The last section lists types of objects that have this event.

We use this information to create a function and a "connection" line in our script.

Traditionally, such functions are called "handlers" and are named "on{Event Name}":

local function onTouched()

The arguments that will be passed by the event are given by "part". This means that "part" will be passed and the type will be Instance (which all Parts are).

To allow our function to use that, we defined it as:

local function onTouched(otherPart)

Finally, we can used the last piece of information to make a connection to that function:



local part = workspace.Part -- your part here

local function onTouched(otherPart)
     otherPart.Transparency = 1 -- make part that touched invisible
     part.BrickColor = BrickColor.Random() -- give this part a new random color

part.Touched:connect(onTouched) -- call onTouched each time something touches


Connections are also automatically removed if:

  • The called function generates an error before the first wait() call.
  • The script itself is removed or reparented.

Note: The connect method does not check that the argument is a valid function. If you misspell the name or otherwise give an invalid function you'll get an error when the event next triggers:

Fri Apr 23 21:50:48 2010 - attempt to call a nil value

Notice that the error has no script reference or line number. This is because it comes from GoodBlox itself inside the Event generation code.

Advanced Usage

Any function name is valid:

local function blah()
     print("In event")

Using anonymous functions in connect is common:

     otherPart.Transparency = 1
     part.BrickColor = BrickColor.Random()
end) -- end for function, close parenthesis for connect

You can also use anonymous functions to change the arguments:

player.Chatted:connect(function(message, recipient)
     onChatted(player, message) -- new arguments need to match onChatted function call

You can create connections at any time even inside of event functions:

local function onChatted(player, message)
     print(player.." said "..message)

local function onPlayerAdded(player)
     player.Chatted:connect(function(message, recipient)
          onChatted(player, message)



The connection object is a special object returned by the connect() method of an Event.


It has 1 method:


The connection object disconnect method, removes the association created by connect(). The defined function will no longer be called when the Event occurs.


-- turn on connection
local conn = part.Touched:connect(onTouched)

-- later to turn off connection