Hi @loleris ,
first of all, I wanted to thank you for this amazing module!
I’ve decided to use it in my project, but I’m encountering an annoying issue and wanted to ask if I’m using sessions incorrectly.
Most of the code is currently based on the tutorial provided in the ProfileStore documentation, but I’m sharing it here anyway:
local function startProfileSession(player)
local profile = PlayerStore:StartSessionAsync(`{player.UserId}`, {
Cancel = function()
return player.Parent ~= Players
end,
})
if profile ~= nil then
profile:AddUserId(player.UserId)
profile:Reconcile()
profile.OnSessionEnd:Connect(function()
print(`Profile session has ended ({profile.Key}) - Profile.Data will no longer be saved to the DataStore`)
Profiles[player] = nil
player:Kick(`Profile session end - Please rejoin`)
end)
if player.Parent == Players then
Profiles[player] = profile
print(`Profile loaded for {player.DisplayName}!`)
else
-- The player has left before the profile session started
profile:EndSession()
end
else
-- This condition should only happen when the Roblox server is shutting down
player:Kick(`Profile load fail - Please rejoin`)
end
end
function DataStore.loadPlayerData(player)
startProfileSession(player)
playersData[player.UserId].profileData = Profiles[player].Data
print(playersData[player.UserId])
end
function DataStore.disconnectPlayer(player)
local profile = Profiles[player]
if profile ~= nil then
profile:EndSession()
end
playersData[player.UserId] = nil
end
This is the function I call in PlayerRemoving
, which should still be triggered when TeleportAsync
is used.
Players.PlayerRemoving:Connect(function(player)
DataStore.disconnectPlayer(player)
end)
The issue seems to occur when a player is teleported to another place in the experience using TeleportService:TeleportAsync(...)
, because it looks like the player’s session isn’t being properly closed. In fact, as soon as the player returns to the current place (which is the hub), ProfileStore doesn’t load any of their data.
Am I handling EndSession
incorrectly? Or do you have any idea where the problem might be?
Thanks in advance!