I’ve recently noticed that once i shutdown the game data saves more than supposed to, It works perfectly fine when the player is leaving etc. How would i fix this or Find an alternate method for saving on game shutdown.
/BindToClose Script\
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
print(player.Name)
sub(function()
SaveData(player)
end)
wait()
end
wait(5)
end)
/Player Leaving Script\
Players.PlayerRemoving:Connect(function(plr)
if isShutdown then return end
SaveData(plr)
end)
/Main Script\
local function SaveData(plr)
if RunService:IsStudio() then return end
print(plr.Name.." DataSaved")
local MaxRetries = 3
local Stats = PlayersData:FindFirstChild(plr.UserId)
if not Stats then return end
if not Stats:FindFirstChild("Ready") then return end
local DataTable = {}
for i,v in pairs(Stats:GetChildren()) do
if not v:IsA("Folder") and v.Name ~= "Ready" and v.Name ~= "Bypass" and v.Name ~= "AEBypass" then
DataTable[v.Name] = v.Value
end
end
local QuestTable = {}
for i,v in pairs(Stats.Quest:GetChildren()) do
QuestTable[v.Name] = v.Value
end
DataTable["Quest"] = QuestTable
local ItemsTable = {}
for i,v in pairs(Stats.Items:GetChildren()) do
table.insert(ItemsTable, v.Name)
end
DataTable["Items"] = ItemsTable
for i = 1, MaxRetries do
local success, err = pcall(function()
DataStore:UpdateAsync("Player-"..plr.UserId, function()
return DataTable
end)
end)
if success then
break
end
end
end