Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this script inconsistent?

Asked by
ked2000 15
9 years ago

I am making a ModuleScript that allows other scripts to share functions easier and such. However, when I run Script a, sometimes it works and sometimes it doesn't. Is this a problem with ROBLOX's interpreter or my script(if so, please tell me the issue)?

Script a

require(workspace.engyn)();

include("b");

sayHi();

Script b

function sayHi()
    out("hi!");
end

require(workspace.engyn)();

ModuleScript engyn

local documents = {};
local appc = require(script:FindFirstChild("appc"));

local M = function(...)
    local env = getfenv(2);

    local App = {
        Environment = {
            out = function(...)local info = {...};for i, v in pairs(info) do print("Engyn("..env["script"]:GetFullName()..") :: "..tostring(v));end end;

            include = coroutine.wrap(function(...)
                local function getDoc(id)
                    return documents[id];
                end

                local function loadDoc(doc)
                    for i, v in pairs(doc.Environment) do
                        if (type(v) == "function") then
                            if (not env[i]) then
                                env[i] = v;
                            end
                        end
                    end
                end

                for _, id in pairs({...}) do
                    local doc = getDoc(id);
                    if (doc) then
                        loadDoc(doc);
                    else
                        if (not getDoc(id)) then
                            repeat
                                wait()
                            until getDoc(id);
                        else
                            return true;
                        end
                    end
                end
            end);
        };
    }

    --Irrelevant code...

    App.New = function(env, args)
        local app = setmetatable(App, Meta);
        app.script = env["script"];

        app:init(env);
        app(args);
        return app;
    end

    documents[env["script"].Name] = App.New(env, {...});

    for i, v in pairs(App.Environment) do
        env[i] = v;
    end

    return documents[env["script"].Name];
end

return M;

Answer this question