Module won't load within reasonable time

I have my ModuleScript that I would like to run before a PlayerAdded event, but it never runs fast enough any idea on how to fix that issue?

local ServerStorage = game:GetService("ServerStorage")

local startTime = os.time()

local StatsService = require(ServerStorage.StatsService)
local newBranch = StatsService.newBranch(false, "game", true)

game.Players.PlayerAdded:Connect(function(plr)
	print("joined")
	local timePlayed = newBranch.instant("TimePlayed","NumberValue",500,plr)
end)

local deltaTime = os.time() - startTime
print("server: "..deltaTime) -- Prints 3

local service = {}
local DataStoreService = game:GetService("DataStoreService")

local debug_name = "ææ_debug_ææ"
local debugDataStore = DataStoreService:GetDataStore(debug_name)

local ServerStorage = game:GetService("ServerStorage")

local Stats_Temp

warn("[StatsService]: INITIALIZING STATS SERVICE")

local function loadDataStore(key : string)
	local result
	local DataStore = DataStoreService:GetDataStore(key)
	
	local succ,err = pcall(function()
		result = DataStore:GetAsync(key)
	end)
	
	if succ and result then
		return result
	else
		warn("[StatsService]: Could not load any data.\n"..tostring(err))
		return nil
	end
end



service.newBranch = function(isLeaderboard : boolean, key : string, isSaved)
	local startTime = os.time()
	--if key == debug_name then
	--	return error(debug_name.." is a locked data key.")
	--end
	
	local data

	local modules = {
		instant = function(name : string, className : string, value, plr : Player )
			if ServerStorage:FindFirstChild("Stats_Temp") then
				Stats_Temp = ServerStorage:FindFirstChild("Stats_Temp")
			else
				Stats_Temp = Instance.new("Folder")
				Stats_Temp.Name = "Stats_Temp"
				Stats_Temp.Parent = ServerStorage
			end

			local newBranchFolder

			if Stats_Temp:FindFirstChild(key) then
				newBranchFolder = Stats_Temp:FindFirstChild(key)
			else
				newBranchFolder = Instance.new("Folder")
				newBranchFolder.Name = key
				newBranchFolder.Parent = Stats_Temp
			end

			if not data.result[plr.UserId] then
				data.result[plr.UserId] = {}
			end

			data.result[plr.UserId][name] = {
				name = name,
				className = className
			}

			if not isLeaderboard then
				local plrFolder

				if not Stats_Temp:FindFirstChild(plr.UserId) then
					plrFolder = Instance.new("Folder")
					plrFolder.Name = plr.UserId
					plrFolder.Parent = newBranchFolder
				else
					plrFolder = Stats_Temp:FindFirstChild(plr.UserId)
				end

				local newInstance

				if plrFolder:FindFirstChild(name) then
					newInstance = plrFolder:FindFirstChild(name)
					newInstance.Value = value
				else
					local succ,err = pcall(function()
						newInstance = Instance.new(className)
						newInstance.Value = value
						newInstance.Name = name
						newInstance.Parent = plrFolder
					end)

					if succ and newInstance then
						return newInstance
					else
						warn("[StatsService]: Could not create new stat.\n"..err)
						return nil
					end
				end


			end
		end,
		clear = function(plr)
			if plr then

			else
				warn("[StatsSerivce]: Clearing Stats")
			end
		end,
		refresh = function()
			data = loadDataStore(key)
		end,
	}
	
	modules.refresh()
	
	if not data then
		data = {configuration = {["isLeaderboard"] = isLeaderboard}, result = {}}
	end
	
	local deltaTime = os.time() - startTime
	print(deltaTime) -- 2 Seconds
	return modules
end

game:BindToClose(function()
	local succ,err = pcall(function()
		debugDataStore:SetAsync("initialize",false)
	end)
end)

warn("[StatsService]: INITIALIZED STATS SERVICE")

return service

1 Like

Just put a

repeat task.wait(0.01) until StatsService

before the player added event, or before you call any module functions if any.

That wouldn’t work quite as the problem I am facing is that “PlayerAdded” Event will not register before I test the game, meaning when I Join in it registers after I have joined.

Isn’t that how a player added event works, running after you join the game?
Sorry, but could you explain your issue a bit more?

So for an example, you test the game but the “PlayerEvent” will not load quickly in time enough to run the function, so if you have a print statement in the “PlayerEvent”. It will not run that print statement, because it registers it too slow.

Try a task.spawn or a coroutine to require the module or just to run the function

People should not be creating new threads just to require a module. They should edit inside the module to make a task.spawn, or remove the yield entirely.

My bad, didn’t realise my mistake

You can fix this by firing PlayerAdded for anyone who joined before connecting the event:

local function PlayerAdded(plr)
	print("joined")
	local timePlayed = newBranch.instant("TimePlayed","NumberValue",500,plr)
end

for i, player in game.Players:GetPlayers() do — loop through each player
PlayerAdded(player)
end
game.Players.PlayerAdded:Connect(PlayerAdded)
1 Like