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