Problem: The line were I call SetAsync to save players data apears to be canceling the thread as nothing is getting printed after that line is reached. I added prints for debugging and commented out the line that connects on player leave. This is because Im in studio and I have a bind to close calling the save player data on server shutdown. What is happening is CHECK1 and CHECK2 will always be called, but the data does not save, and CHECK3 is never reached.
Here is the relevant code:
local DataStoreService = game:GetService('DataStoreService')
local database = DataStoreService:GetDataStore('Data')
-- Save players data
local function savePlayerData(player)
-- So data is not overwritten incorrectly
if sessionData[player.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
print("CHECK1")
repeat
print("CHECK2")
-- Save players leaderstats
success, errorMsg = pcall(database.SetAsync, database, player.UserId, sessionData[player.UserId])
print(success, errorMsg)
print("CHECK3")
print(errorMsg)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
print("CHECK4")
-- After user data saved, increment global
updateGlobalData(player)
end
end
--game.Players.PlayerRemoving:Connect(savePlayerData)
-- Call savePlayerData on server shutdown
local function serverShutdown()
--if RunService:IsStudio() then return end
for _, player in Players:GetPlayers() do
task.spawn(function()
savePlayerData(player)
end)
end
end
game:BindToClose(serverShutdown)