Question about coroutines

Yo, so I’m making a FNaF game right now and I’m planning on using coroutines for the nights because I heard they are pretty useful and I have some questions for it.

I’m planning a system where I would have module scripts for each enemy in a folder where I would require them in the main script and have a function where they would activate when the player starts the night. My problem is that I want it so that whenever the animatronic kills the player at night, I want the main script to recognize that and cancel all the other enemy script and exit the coroutines and then start a game over function, but as a noob with coroutines I cannot achieve this

My script structure is sort of like this:

  1. Player fires remote event to start game
  2. Server starts game and functions all the modules for enemy
  3. I need help with this part, where if a single enemy kills the player, it breaks from the loop
  4. Game resets and goes to main menu

Thanks for reading :sob::sob::sob:

1 Like

Sounds like a game round system with an asynchronous race condition where to break the loop there are multiple possible factors.

I suggest using Promises which internally uses coroutines but comes already structured to handle sequence of events.

To break the loop you will need a cleanup function, in my case its a maid class which just calls :Destroy() on unneeded instances.

1 Like

Thanks a lot :smiling_face_with_three_hearts:. Just wondering is there a way to like make this but without promise since I don’t generally use third party scripts unless necessary.

You can try making something similar using coroutine.yield, coroutine.resume(), coroutine.close() to create your own custom promise basically.

On the plus side you will probably understand it even better this way if you do.

Or task.spawn and task.cancel would also work.

Thanks, if I complete the game I’ll be sure to put you in credits :wink:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.