Usage of require() not functioning as intended

Greetings, everyone.

Let me provide you an outline for this little project of mine. I intend on using a single server script to handle the majority of the game functions, via the usage of module scripts. To use a module script, as you all know, you must use require(ModuleScript) to use it in a functional manner. However, for some reason when I am attempting to do this is just stops. No errors are returned, it just stops.

--:// Services
local serverScriptService = game:GetService( 'ServerScriptService' );
local replicatedStorage = game:GetService( 'ReplicatedStorage' );
local serverStorage = game:GetService( 'ServerStorage' );
local workspace = game:GetService( 'Workspace' );
local textService = game:GetService( 'TextService' );

--:// Constants
local framework = serverScriptService:WaitForChild( 'ThamesValleyFramework', 320 )

--:// Modules
local modulesCollection = framework:WaitForChild( 'Modules', 320 )
local modules = {}

--:// Initialise
for _, Module in pairs (modulesCollection:GetDescendants()) do
    if Module:IsA( 'ModuleScript' ) then
        print( 'Attempting to load '..Module.Name..'...' );
        local timeDiff = os.time();
        local initialModule
        local success, err = pcall(function()
            print("Requiring the module...") -- This prints as expected
            initialModule = require(Module);
            print("Module required.") -- This never prints, nothing else continues
            initialModule:LoadModule();
        end);

        if success then
            local timeDiff2 = os.time() - timeDiff;
            table.insert(modules, Module.Name, initialModule);
            print( 'Successfully loaded '..Module.Name..' in '..timeDiff2..' seconds.' );
        else
            if err then
                warn( 'Failed to load '..Module.Name..'. '..err );
            else
                warn( 'Failed to load '..Module.Name..' due to an unknown error.' );
            end
        end
    end
end

Does anyone know the issue and, preferably, how to go about solving it?

Do your modules have some kind of wait in their return that could be delaying progression in the script? Can you show us one of the modules that cause this issue?

1 Like
--// Services
local serverScriptService = game:GetService( 'ServerScriptService' );
local replicatedStorage = game:GetService( 'ReplicatedStorage' );
local serverStorage = game:GetService( 'ServerStorage' );
local workspace = game:GetService( 'Workspace' );

--:// Constants
local signalController = {}
local signalsCollection = workspace:WaitForChild( 'SignalsCollection', 320 );

--:// Variables
local signalColours = {
    signalRed = Color3.fromRGB( 231, 32, 32 );
    signalYellow = Color3.fromRGB( 224, 242, 21 );
    signalGreen = Color3.fromRGB( 52, 232, 39 );
};

--:// Functions
function updateSignalAspect( signal )
    if not signal then
        return;
    end


end

function updateSignalStatus( signal )
    if not signal then
        return;
    end

    
end

function signalController:LoadModule()
    print("Loaded.");
end

--:// Returned Module
return signalController

There are no waits or anything. In this case, I am just asking it to print something. No processing of data or any time-wasting activities, currently.

Edit → I haven’t finished programming any module, I was making the initial framework first to ensure it works.

In addition to what @EmbatTheHybrid said when working with modules just be aware of circular dependencies which also cause errors, is this the only module?

That’s false theres a potential wait right there, personally I would immediately go into print debugging this out which you haven’t seem to done yet.

print("Signals collection")
local signalsCollection = workspace:WaitForChild( 'SignalsCollection', 320 );
print("Signals collection, success")
2 Likes

I was referring to waits in the modules.

I know that the line about signalsCollection works because print( 'Attempting to load '..Module.Name..'...' ); is printing.

It is the only module, in addition I know that if I have module1 requiring module2 and module2 requires module1 it won’t do anything.

@dthecoolest has the most likely cause of your problem. All I’m doing is providing advice.

In your code you have it where the LoadModule function is required when the module is first required. However, this is unnecessary, as all code in the module only runs once when it is initially required. You could just put any code inside the LoadModule function inside a do-end block.

Can you elaborate on the end of that, please?

Correction on my original response.

Hold my hands up, I was an idiot. That folder didn’t exist.

Always be sus of any waits and wait for childs.

In addition I do not think pcall is a good idea or wait for child 320

local modulesCollection = framework:WaitForChild( 'Modules', 320 )

  1. Theres no reason to wait in a server script since everything is loaded.
  2. Even if you do need to wait for something that hasn’t existed you probably can use a event listener to listen for when it is created
  3. Without the timeout or pcall you would have been able to see Infinite yield possible error
1 Like

I appreciate the clarification here, I’ll be sure to apply this in the future.

1 Like

All code is only run once when the module is initially called. Calls after that will just return the value that was originally returned. This means that this:

function signalController:LoadModule()
    print("Loaded.");
end

Can just be this:

do
	print("Loaded.");
end
1 Like