I’ve adopted the Madwork framework of writing code. I have a ModuleScript called Madwork
which contains useful ModuleScripts for my game. Madwork returns the table Madwork
as well as assigning _G.Madwork = Madwork
, so that any Script/LocalScript can do things like this at the top of the code:
local Madwork = _G.Madwork -- Framework saved in Global table
local Promise = require(Madwork.GetShared("Promise") -- Easy require
The problem is that asynchronous functions within _G.Madwork
like :WaitForChild(module_path)
mean that other scripts try to index _G.Madwork
before it has even been assigned. I have to do something like:
wait(5) <-- Wait for ModuleScripts to load e.g. ReplicatedStorage.Promise
local Madwork = _G.Madwork <-- Framework saved in Global table
local Promise = require(Madwork.GetShared("Promise") <-- Easy require
Otherwise I will be assigning the Madwork variable before the ModuleScript has even returned anything:
local Madwork = nil -- Not good
Is there a clever way to ensure that when I assign:
local Madwork = _G.Madwork
That it actually already exists in the first place, otherwise yield the script?
As of writing this post, this has fixed it for me:
repeat task.wait() until _G.Madwork ~= nil
local Madwork = _G.Madwork
Is there still an easier and neater way?