I am making a sandbox building game and I have been saving the players data by them clicking a gui button and sending it to the server to serialize but in an attempt to create a better user experience I want to make it autosave when the player leaves. The problem is, the data takes a couple seconds to fully save so by the time the data saves the server has shut down. (1 player servers) I’ve tried using PlayerRemoving and looking over some posts here but to no success. Any ideas will help!
local function onShutDown() task.wait(1) end
Should I use this with the PlayerRemoving function?
If I remember correctly,
Players.PlayerRemoving might not fire if the player in the question is the last one that is leaving the game, which is why you should use
game:BindToClose() instead as that would give you up to 30 more seconds before server shuts down completely.
However, if your data serialization algorithm is taking longer than 30 seconds then I would look at the algorithm itself and try to optimize that first if I were you.
Great I’ll try that! Thank you
okay im still having trouble with this, i cant figure out why the function is not working
local slot = script.Parent.Parent local foldernum = workspace.Slot.Folders.Value local plr = game.Players:FindFirstChild(workspace.Owner.Value) --print("testing") game:BindToClose(function() for i,v in pairs(workspace.Slot:GetChildren()) do if v:IsA("Folder") then local foldername = v.Name print("Saved "..foldername) local DS = game:GetService("DataStoreService"):GetDataStore(foldername) local Save = require(game.ServerScriptService.Serializer) if plr.Name == workspace.Owner.Value then local result, problem = pcall(function() DS:SetAsync(tostring(workspace.Owner.OwnerID.Value), Save.Encrypt(v:GetChildren())) end) if not(result) then error(problem) end end end wait(1) end script.Disabled = true end)