Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to connect without an event?

Asked by
emite1000 335 Moderation Voter
9 years ago

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?

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
0
If I do the checking solution with wait(), will it cause the game to be slower? At least noticeably? emite1000 335 — 9y
1
Depending. If your check is something complicated (slow) then that could be a problem. For many things, you probably don't have to react within a 30th of a second, so a longer wait time will work just as well as be much more efficient. If there are *several* / *many* of these scripts doing `while wait()` loops, there *WILL* be a problem pretty quick. BlueTaslem 18071 — 9y
Ad

Answer this question