How to get events to process in a specific order

So I have a few questions, and I’ll just string them together.
I am creating a bootstrap script so to speak so that my scripts and classes can run/initialize in a certain order since some methods are dependent on others existing. If I took an asynchronous approach, it would become confusing and difficult to read and leads to unnecessary code in the future.

I have tried using bindable/remote events/functions but they don’t seem to work properly.
I do understand from documentation that events are one way so I switched to functions with callbacks.
There were few problems I ran into with this implementation.

  • Scripts seem to run at the same time yet one script runs quicker than another despite having more code and more computations.
  • For some reason my code seems to skip over the callback for some reason and jump to the function call following Bindable invocation.
  • If I started adding in delays, first its just poor coding design, and two it would lead to performance/runtime issues down the road.
  • I tried creating an instance of the bindable inside of my function but for some reason that seems to be the first thing created since my WaitForChild events/functions run before the instance is created?

My second question is if on Run Bindable Events or Functions automatically run once when the script is first executed. The reasoning for this is because the functions are being called inside of these Bindables and remotes when I don’t have any places in any of my script files where I am calling them ahead of time.

  • One reason I ask of this is because I only call the function once through the Bindable, yet the function is running twice.

My third question is yesterday, I posted a question regarding Singletons where I just want a single instance of my ModuleScript. The problem I ran into were cyclic tables. They stated the solution was to not set it in the metatable. Despite trying that solution, I still get the same error. Are there any other solutions?

Here is some sample code where I get the error:
RepoManagement Module cyclic table error (2nd line)

local RepoManagement = {}
RepoManagement.__index = RepoManagement
RepoManagement.ClassName = "RepoManagement"

function RepoManagement:Init()
    dss = ServerStorage.Bindables:WaitForChild("DataStoreService"):Invoke() --Line with the Error
    ...
    return self
end

DataStoreService Module referred to

local DataStoreService = {}
DataStoreService.__index = DataStoreService
DataStoreService.ClassName = "DataStoreService"

function DataStoreService:New()
    Log = ServerStorage.Bindables:FindFirstChild("Log"):Invoke()
    return self
end

Did you try to use promises yet? It is really useful when you want to perform tasks in a specific order

1 Like

I don’t think that fits the implementation I am looking for.