I’m trying to prevent data loss by making it so people’s stats won’t load until it has been confirmed they save.
This code should 100% work, but I’m sure it will slow down the datastores.
local session=game:GetService("DataStoreService"):GetDataStore("Sessions")
local function save(plr)
-- save stats
session:SetAsync(plr.UserId,false)
end
game.Players.PlayerAdded:Connect(function(plr)
local insession=session:GetAsync(plr.UserId)
if insession then plr:Kick("Your data is being loaded.") return end -- would ideally write a wait loop here instead of kicking them
session:SetAsync(plr.UserId,true)
repeat plr.AncestryChanged:wait() until not plr.Parent
save(plr)
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
if v.Parent then
save(v)
end
end
end)