Interupt Script

Coroutine-Based Interupt Module

When writing Roblox code, you sometimes want to wait for multiple signals to fire before continuing, currently you can do something like this with Bindables, however, Bindables and Instances can take up a lot of memory, and have to be cleaned up correctly.

The solution? A coroutine based Interupt script that doesn’t touch a single bindable, ever.

API:

Interupt returns a new function that you can call to get a new interupt

The Interupt object then exposes three methods:

Interupt:Release()

Releases the interupt and then destroys it, should be used inside your main interupt coroutine.

Interupt:Wait(function interuptCoroutine)

Yields the current thread, runs a coroutine, and waits until the coroutine calls Interupt:Release()

Interupt:Destroy()

Destroys the interupt, used to cleanup some internal storage tables.

WARNING: Once an interupt has been destroyed, do not attempt to Wait again on it, it will error.

Code Example:

I cant think of a good code example, but here’s how you’d use the API: (if you have a better code example pls provide it below)

local InteruptLibrary = require(script.Interupt) --gets the library

local interupt = InteruptLibrary.new() --creates a new interupt
interupt:Wait(function() --yields the thread
  --waits for all events to fire
  local events = script.Events:GetChildren()
  local c = 0
  local cMax = #events

  for _, v in ipairs(events) do
    local connection
    connection = v.Event:Connect(function()
      c+=1
      if c == cMax then
        interupt:Release() --releases the interupt and lets the code continue
      end
      connection:Disconnect() --we only want this to run once for each event
    end)
  end
end)

interupt = nil --discards the interupt as it's been destroyed (you should do this if it's in the global stack)
print("i will never run until all events fire")

Code:

Code is available at this gist post
https://gist.github.com/FilteredStudio/e1adb3e5a8e7996c06d59155afe00952

1 Like

So is this more of just a wrapper for coroutine?