For some reason, when I close the game, my data stores don’t save, and instead it just ignores the saving line of code and just continues the script.
local function saveData(plr)
if onSaveCooldown[plr] then return end
onSaveCooldown[plr] = true
local success, errorMessage = pcall(function()
print("run hello")
print(playerData[plr]) --print the data to make sure it exists
playerDataStore:SetAsync(tostring(plr.UserId) .. "|" .. curDate,playerData[plr])
print("wow")
end)
print(success,errorMessage)
if gameClosing then task.wait(5) return end
task.delay(15,function()
onSaveCooldown[plr] = false
end)
end
Code that calls the function
game:BindToClose(function()
gameClosing = true
for _,plr in game.Players:GetPlayers() do
saveData(plr)
end
print("done saving everyhting")
end)
Expected output:
“run hello”
prints the correct data
“wow”
true nil
“done saving everything”
Actual output:
“run hello”
prints the correct data
“done saving everything”
The weird thing is that if I call that function during runtime, then it works fine. But specifically on game close it doesn’t work.
Any advice?
Removing the p-call doesn’t do anything the script still stalls at the setAsync
I’ve changed to UpdateAsync but it still has the same problems(doesn’t even run the code inside the passed function as well)