How to efficiently load modules on the client-side?

So I am trying to load a bunch of modules through a main module that is being loaded by a local script in StarterPlayerScripts. My current method of loading said modules is looping through them and requiring each one and calling in their main method(that is shared through all of the modules) :init(player: Player).

The Problem that I am currently facing is the fact that the modules(7 in total), do not fully load or partially load or only one or two load, causing a lot of issues since all these modules are for the User Interface. I’ve tried adding delays while looping through each module, loading each one manually, waiting for the player’s character to spawn-in, etc.

Replicated Storage and Player Scripts Workspace

image

image


Main Module Loading Protocol (UserInterface)
function UI:handleExternal()
	print("[CLIENT]: Loading external modules...")
	
	if (LocalPlayer) then
		for _,module in pairs(script:GetChildren()) do
			warn("[LOADING]: " .. module.Name)

			local moduleReq = require(module)
			moduleReq:init(LocalPlayer)

			task.wait(0.11)
		end
	end
end

--= Initializers =--
function UI:Init() : nil
	
	self:handleStats()
	
	Mouse.Button1Down:Connect(function()
		self:manageClicking()
	end)
	
	self:handleButtons()
	self:handleButtonEffects()
	
	self:handleGuiClosing()
	
	self:handleExternal()
	
	require(script.Parent.Destructible):init(LocalPlayer)
end
StartPlayerScripts Localscript (UserInterfaceSetup)
--= Services =--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

--= Object Refrences =--
local Libraries = ReplicatedStorage.Libraries
	local ClientLibraries = Libraries.ClientLibraries
	local SharedLibraries = Libraries.SharedLibraries

--= Dependencies =--
local UserInterface = require(ClientLibraries:WaitForChild("UserInterface"))

--= Init =--
UserInterface:Init()

im not sure modules in-game can be require()ed from client
if im wrong please tell me

You are correct, a user cannot load a require with an asset(id) from the client side.

but are we acually able to require() module from client that is stored in game??? idk

require() cannot load a module with an asset ID outside of the game on the client.
You have to use it on the server for this.

Loading modules inside the game works both on the server and client though.

1 Like