Sending a signal to fire a function

Hey there!

So when a function ends, i want another one to start. However, i want the new function to be separate from the function which ended.

One way of doing this would be by firing a remote event, and have a event listener which connects the new function when the event is fired from the function that ended.

Is there any other more efficient way to do this?

Any help is appreciated.

1 Like

Either execute the new function in a coroutine whenever you need too, or just connect an event listener that’s all - since you want them separate, which I assume means you want them in different threads.

1 Like

Yes you could use BindableEvents, spawn() or fastspawn technique.
May I ask what you’re trying to accomplish? Is the chain of events dynamic?

2 Likes

@XxELECTROFUSIONxX @Club_Moo

I’m trying the methods you have listed right now, but just for clarification here’s what i mean:

So my code is something similar to this (the layout):

local function example1()
   examplecode
end

local function example2()
   examplecode
end

I want example 2 to be connected when example1 ends. However, i don’t want this:

local function example1()
   examplecode
   wait(exampletime)
   example2()
end

because then when example2 ends, it goes back to example1 to run any remaining code. That’s what i mean by separate. If i were to use the remote event function example, it would be separate, because when example2 ends, that’s it.

I’m just wandering if there is a simpler way to do this instead of the remote event example. Please keep in mind i wan’t this to be all running off one thread. (I’m making a Intermission/Round system)

It seems like you may be contradicting yourself. Questions:

  • Do you only have 2 functions? Or are there a chain of them?
  • You say “it goes back to example1 to run any remaining code.” Do you want that code to run?
  • “However, i don’t want this” What about it do you not want?
  • “i wan’t this to be all running off one thread” It seems like you do want what you said you didn’t?!
  • Can you give a little more detail of what you do want and what you don’t want?
2 Likes

put example2() at the very end of that block

1 Like

Answers:

  • I have a chain of functions. I wan’t each of them to run one by one, by being connected by the function that ran before them.

  • I do not want that code to run.

  • I don’t want for example2() to be connected inside example1() because when example2() ends, it connects another function which in the end causes a whole chain of functions, all leading back to example1()

  • By one thread i mean one continuous chain of functions, which fire the next function in the chain when the function has finished.

  • Basically i want a continuous chain of functions, starting from function 1. When function1 finishes, i want function2 to start, however i want function2 to be separate from function1 because of answer 3

If you have any other questions, please feel free to ask.

Are you looking for something like Promises?

1 Like

Okay, but is it dynamic? What I mean by that is does it always go eg:
fn1() → fn2() → fn3() → fn4() → fn1()?
Or could it also possibly for instance be eg:
fn1() → fn3() → fn4() → fn2() → fn1()?

1 Like

example1() gets disconnected and reconnected by another system in the script. If i were to disconnect example1(), it would go straight onto example2(), even though example1() wasn’t fully finished. That’s why i need a chain of functions that go onto the next one after one has finished.

For clarification, that ‘other system’ goes through all the players in the game, and checks for a value to be true. If less than 2 people have that value to true, interrupt the Intermission function, and make it restart. If 2 or more people have the value to true, then don’t interrupt the intermission, and if the intermission manages to finish without being interrupted, that system is deactivated, and the round begins (function 2). There’s more to it, and what i’ve said so far doesn’t really make sense, but you get the idea.

Wouldn’t those two examples be the same thing?

Anyway i believe it is dynamic. It should be something like this:

Intermission() → Teleport players() → RoundSystem() → Intermission()

Sounds like you just need one more level of indirection such as:

function example0()
    local ok = true
    repeat
        Intermission()      -- example1()
        TeleportPlayers()   -- example2()
        ok = RoundSystem()  -- example3()
    until not ok
end
2 Likes

Intermission() needs to be separate and needs to be the one connecting example0 because of this:

Just to remind you, it’s ok if you don’t know a better way to do this than a remote event. This post is only here incase there is a simpler way.

You dont need to make separate functions for each of them… Just do this all inside a while loop.

1 Like

If by “interrupt the Intermission function, and make it restart” you mean call Intermission again,
you can simply have Intermission() return a value that represents that it was interrupted, and
just put another loop within the loop that calls Intermission(), and break out of it if it can move on
to the next function.

1 Like

Something like this?

local e = Instance.new("BindableEvent")

local function p(e)
     e:Fire()
end

e.Event:Connect(function()
      p(e)
end)
2 Likes

I think its better if i just keep it as it is. Tbh i can’t even remember why i wanted to separate those two functions. I think i was just haunted by the fact that my game’s core game mechanics scripts would pretty much rely on one function.

I did find out a method, which is to use :GetPropertyChangedSignal on a value to trigger function2, but it’s probably better to not separate the functions.

It sounds like i’m absolutely crazy and don’t know what i’m talking about, but it’s hard to explain.

I still appreciate the help tho guys. :+1:

1 Like