Recently in the game I've been working on, when I migrate servers to a new update or if the player rejoins immediately after leaving, ProfileStore:LoadProfileAsync()
takes around a full minute load. It loads basically immediately in all other cases. This didn't start happening until very recently and I'm not sure what I've done to cause this as I haven't added any new data to be saved/loaded recently.
As I'm pretty unfamiliar with how ProfileService works I'm wondering what the cause of this long load time could be and how I could go about fixing it.
local Players = game:GetService("Players")
local serverScriptService = game:GetService("ServerScriptService")
local Template = require(serverScriptService.PlayerData.Template)
local ProfileService = require(serverScriptService.Modules.ProfileService)
local ProfileStore = ProfileService.GetProfileStore("Test", Template)
local function PlayerAdded(player: Player)
print("TEST1")
local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
print("TEST2")
-- Cut out a lot of code that I don't think is relevant
end
Players.PlayerAdded:Connect(PlayerAdded)
The first print fires as soon as the player joins, and the second doesn't fire until ~a minute after in the cases I mentioned above.
Players.PlayerRemoving:Connect(function(player)
local profile = Manager.Profiles[player]
if not profile then return end
profile:Release()
end)
game:BindToClose(function()
for _, profile in Manager.Profiles do
profile:Release()
end
end)
I do have these functions to handle releasing profiles upon players leaving or the server closing.
Ok so I found out a method that fixed my load times. I had my data script create a BoolValue
and parent it to each player when they join called hasLoaded
. I set it to true after the profile loaded and stopped my other scripts from doing anything until hasLoaded == true
.
local function PlayerAdded(player: Player)
local hasLoaded = Instance.new("BoolValue")
hasLoaded.Name = "hasLoaded"
hasLoaded.Value = false
hasLoaded.Parent = player
local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId.."_Save5")
hasLoaded.Value = true
-- Unrelated code
end
And here's the script where I manage all of my client scripts:
local player = game.Players.LocalPlayer
local hasLoaded = player:WaitForChild("hasLoaded")
while hasLoaded.Value == false do
task.wait(1)
end
-- Variables for my scripts and calls to their start functions
After doing this, the load times are basically instantaneous again. I'm not sure why this reduced the load times so drastically, but it did!