Client Loading Script Issue

Hello! I’m currently experiencing an issue with my loading module script. What’s happening is that sometimes the game loads properly like such:

image

but some other times the client does not load properly and breaks the whole game:

image

this is my loading module script:

--- Settings
local Debug = true

--- Main
local Library = {
	Loaded = false,
	Game = game:GetService("ReplicatedStorage"):WaitForChild("Game"),
	Debris = workspace:WaitForChild("__DEBRIS"),
	Things = workspace:WaitForChild("__THINGS"),
	Assets = game:GetService("ReplicatedStorage"):WaitForChild("Assets"),
	AnalyticsService = game:GetService("AnalyticsService"),
	AssetService = game:GetService("AssetService"),
	BadgeService = game:GetService("BadgeService"),
	Chat = game:GetService("Chat"),
	CollectionService = game:GetService("CollectionService"),
	ContentProvider = game:GetService("ContentProvider"),
	ContextActionService = game:GetService("ContextActionService"),
	ControllerService = game:GetService("ControllerService"),
	DataStoreService = game:GetService("DataStoreService"),
	FriendService = game:GetService("FriendService"),
	GamepadService = game:GetService("GamepadService"),
	GamePassService = game:GetService("GamePassService"),
	GroupService = game:GetService("GroupService"),
	GuiService = game:GetService("GuiService"),
	HapticService = game:GetService("HapticService"),
	HttpService = game:GetService("HttpService"),
	InsertService = game:GetService("InsertService"),
	JointsService = game:GetService("JointsService"),
	KeyboardService = game:GetService("KeyboardService"),
	Lighting = game:GetService("Lighting"),
	LocalizationService = game:GetService("LocalizationService"),
	LoginService = game:GetService("LoginService"),
	LogService = game:GetService("LogService"),
	MarketplaceService = game:GetService("MarketplaceService"),
	MaterialService = game:GetService("MaterialService"),
	MessagingService = game:GetService("MessagingService"),
	MouseService = game:GetService("MouseService"),
	NotificationService = game:GetService("NotificationService"),
	PathfindingService = game:GetService("PathfindingService"),
	PermissionsService = game:GetService("PermissionsService"),
	PhysicsService = game:GetService("PhysicsService"),
	Players = game:GetService("Players"),
	PointsService = game:GetService("PointsService"),
	PolicyService = game:GetService("PolicyService"),
	ProximityPromptService = game:GetService("ProximityPromptService"),
	ReplicatedFirst = game:GetService("ReplicatedFirst"),
	ReplicatedStorage = game:GetService("ReplicatedStorage"),
	RunService = game:GetService("RunService"),
	SocialService = game:GetService("SocialService"),
	SoundService = game:GetService("SoundService"),
	StarterGui = game:GetService("StarterGui"),
	StarterPack = game:GetService("StarterPack"),
	StarterPlayer = game:GetService("StarterPlayer"),
	Teams = game:GetService("Teams"),
	TeleportService = game:GetService("TeleportService"),
	TextChatService = game:GetService("TextChatService"),
	TextService = game:GetService("TextService"),
	TouchInputService = game:GetService("TouchInputService"),
	TweenService = game:GetService("TweenService"),
	UserInputService = game:GetService("UserInputService"),
	UserService = game:GetService("UserService"),
	VoiceChatService = game:GetService("VoiceChatService"),
	VRService = game:GetService("VRService"),
	DebrisService = game:GetService("Debris"),
	Heartbeat = function(arg) 
		for index1 = 1, arg or 1 do
			game:GetService("RunService").Heartbeat:Wait()
		end
	end,
	Stepped = function(arg) 
		for index2 = 1, arg or 1 do
			game:GetService("RunService").Stepped:Wait()
		end
	end,
	RenderStepped = function(arg) 
		for index3 = 1, arg or 1 do
			game:GetService("RunService").RenderStepped:Wait()
		end
	end,
	LocalPlayer = game.Players.LocalPlayer,
	Random = require(script:WaitForChild("Modules").Random).R,
	Services = require(script.Services),
	Functions = require(script.Functions),
	Balancing = require(script.Balancing),
	Asserts = require(script.Asserts),
	Print = require(script.Print),
	Types = require(script.Types),
	Modules = require(script.Modules),
	Signal = require(script.Signal),
	Network = require(script.Network),
	Variables = require(script.Variables),
	Shared = require(script.Shared),
	DropTable = require(script.DropTable),
	Items2 = require(script.Items2),
	Player = require(script.Player),
	Audio = require(script.Audio),
	Directory = require(script.Directory),
	RAPShared = require(script.RAPShared),
	Util = require(script.Util),
}

--- References
local isServer = game:GetService("RunService"):IsServer()

--- Variables
local debugTag = (isServer and "Server-Sided" or "Client-Sided")


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--- Client specific stuff
if not isServer then
	--- Wait on intro to finish
	repeat game:GetService("RunService").RenderStepped:Wait() until _G.IntroDone
end

-- Debug timer
local _startInitTick = tick()

local IsLoaded
if isServer then
	Library.ServerScriptService = game:GetService("ServerScriptService")
	Library.ServerStorage = game:GetService("ServerStorage")
	local Server = require(game:GetService("ServerScriptService"):WaitForChild("Library"))
	Library.Network = Server.Network
	setmetatable(Library, { __index = Server })

	IsLoaded = function() 
		return Server.Loaded
	end
else
	local Client = require(script.Client)
	Library.Network = Client.Network
	setmetatable(Library, { __index = Client })

	IsLoaded = function()
		return Client.Loaded
	end
end

task.spawn(function() 
	local lateLoading = false
	
	-- Iterate through all child modules
	for _, moduleScript in ipairs(script:GetChildren()) do
		-- Check if the module is a ModuleScript and not already loaded
		if moduleScript:IsA("ModuleScript") and Library[moduleScript.Name] == nil and moduleScript.Name ~= "Client" then
			if not lateLoading then
				-- Delay the first late-loading module by one frame to improve performance
				lateLoading = true
				task.wait()
			end

			print("Library (Shared) - Late loading: ", moduleScript)
			Library[moduleScript.Name] = require(moduleScript)
		end
	end
	
	-- Wait for Client/Server Modules to load
	while not IsLoaded() do
		task.wait()
	end
		
	--- Done!
	Library.Loaded = true
	
	--- Debug
	if Debug then
		print(string.format("✅ %s | Game Modules took %dms to initialize!", debugTag, math.round((tick() - _startInitTick) * 1000)))
	end	
end)

function Library.Load() 
	while not Library.Loaded do
		task.wait()
	end
end


-------

return Library

Any help is greatly appreciated! :+1:

2 Likes

The error is coming from ReplicatedStorage.Libary.Client.PetCmds, Line 15 which tries to access DefaultStats, that script probably tries to get that variable/function before it has been stated and causes the module to throw an error when trying to load it.

Could you please show the script that causes the error in order for me to further assist you?