Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do events and :Connect work in Roblox Lua?

This may be a difficult topic to address, but I have been thinking about it and want to know more. As the question states I am wondering how events work and what causes them. I did a google search on whether or not Lua has events and it said that Lua can be used in that way. But how does that work? Can I create my own events(Might be useful if I could)? Also how do the Roblox events work? What causes them and how does :Connect work? I know how to use events but my question is more: What is behind them and how do they work? Is there code behind events? If so what does it look like? Also I have seen questions involving continuation of events. As in: If I touch this brick how can I tell if I am still touching this brick? The questions I have seen involved a while loop. Is that the best way to go? What is the most efficient way to tell if I am still, say, holding down click on a button? I know that this is a lot to ask, but I am very curious about this topic and hope someone can help me. Thanks!

1 comment

0

By Roblox Lua I do not mean a separate language than Lua. I am referring to the custom events and functions that the Roblox API has added.

2 answers

✓ Accepted answer

Introduction

The Roblox API provides us with RBXScriptSignals (also known as events) which are fired whenever an event take place, and are often members of an Instance. This is an implementation of a software design pattern called the Observer pattern, which you can read more about here.

Listeners

Listeners (aka callbacks, observers or subscribers) are functions that are invoked whenever an event is fired. Invoked is more suitable in this context than called, because when we say we invoke a function, we often talk about calling the function when an event's taken place.

Subscribing to an event.

When we subscribe to an event, we're talking about Connect()ing a listener function to an event. So when the event is fired, the callback function that gets passed is invoked. We can subscribe to an event through the :Connect() member function/method of a RBXScriptSignal.

Connect

  • Method

    RBXScriptConnection RBXScriptSignal:Connect(function(Variant) callback)

  • Sample call

      local Event = workspace.Baseplate.Touched
      local EventSignal
    
      local function EventListener(part)
          print(part.Name)
      end
    
      EventSignal = Event:Connect(EventListener)
    
  • Description

The event is assigned to our Event variable. We forward declare an EventSignal variable, event signals (formally RBXScriptConnections) are the objects that are returned from a Connect() call. Event signals contain a Disconnect() method, which allow us to unsubscribe or stop listening to an event. We pass our callback/listener through Connect(), and the EventListener function will now be invoked in a separate thread (or environment, so to speak) whenever the event is fired. When the event is fired, value(s) are passed relevant to the callback, you can find information about an event on the Roblox wiki.

Wait

  • Method

    Variant RBXScriptSignal:Wait()

  • Sample call

      local Event = workspace.Baseplate.Touched
      Event:Wait()
      print("foo")
    
  • Description

The Wait() method yields the script (pauses execution) until the event is fired, so foo will not be printed to the output until the baseplate is touched. Wait() returns the values it would've passed to a callback function if it were Connect()ed to one.

Creating your own events.

You can implement your own events through the RBXUtility library, or using bindable events. The former is deprecated, but is much closer semantically than bindable events.

Well events run / fire depending on what event you are using. For example if I used a .Touched event it will connect and function once I touch it or anything touches it.

If you were using a event like MouseButton1Down it runs when you left click a button.

Basically event runs on a specific condition similar to if statements. To find more about events Roblox wiki is a good place to explore. Check out objects and you will see that there are events on them. Some services (like RunService) also have some events.

Events can not be created by you can create functions or do things such as : plr.CharacterAdded:Wait() which waits until the event is met.

For holding down a key there is a command for that, using userinput service:

local UIS = game:GetService('UserInputService')

UIS:IsKeyDown(Enum.KeyCode.Q)

end

This is a loop that will run while Q is being held down. Check out UserInputService for more about UserInput.

Hopefully this helped you understand a bit!

7 comments

0

You forgot the 'while' and 'do' in the loop, and also the 'wait()'. Also, you never explained what 'Connect()' does.

0

You can also read up a bit on roblox signals here https://wiki.roblox.com/index.php?title=RBXScriptSignal

0

Thanks for your help but this was not really what I was asking. I was more asking for the theory and what code behind events looks like and efficient ways to implement events and less a specific script or pointers on how to use events.

0

It may be to complex to answer I know. If it is too much to ask just say so.

0

Also why can I not create my own events?

0

Because why would you need to?

0

Amazing

This discussion is preserved as it was. New answers, comments, and votes are closed.