How do I use :connect()
without having an event before it? For example if I had a script that I wanted to say "if this happens, then connect to function", how would I do that?
You can't.
When you have a :
or .
, you are referring to a particular, already defined thing in the object. If it's not a ROBLOX event, it's not going to have a :connect
method.
BindableEvents, though, let you build something which turns a normal function call into an event, which maybe you're more comfortable with, or something.
Those BindableEvents still need to be triggered by something, though. You have to do that triggering. Here are two different ways you might accomplish it.
There isn't really a way in general to make your own sorts of events that are based on conditions.
Usually, though, a system to approximate it will look like this:
while true do wait(); -- Let's just check as frequently as we can if SOME_CONDITION_IS_TRUE then doThatOneThingWeWant(); end end
That is, we just constantly check to see if the conditions are right, and then call the appropriate function.
Alternatively, instead of checking constantly for changes, we could hook into an actual event that might sometimes indicate what we are waiting for.
For example, if we have an event for an object whose name is a prime number to appear in the workspace, we could just hook into workspace
's ChildAdded
event and do the check ourself:
workspace.ChildAdded:connect( function(child) if SOME_CONDITION_ABOUT_CHILD_AND_OTHER_STUFF then doThatOneThingWeWant(); end end