Perhaps an attribute debounce? The first thing that comes to mind is adding the player to a table or assigning an attribute to the player and removing it after several seconds to prevent multiple save attempts.
local Saving = {}
game.Players.PlayerRemoving:Connect(function(plr)
if table.find(Saving, plr) then return end
table.insert(Saving, plr)
task.delay(5, -- time before the player can save again
table.remove(Saving, table.find(Saving, plr))
)
SaveData(plr)
end)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
if table.find(Saving, plr) then continue end
table.insert(Saving, plr)
task.delay(5, -- time before the player can save again
table.remove(Saving, table.find(Saving, plr))
)
SaveData(plr)
end
end)
Or I reccommend ProfileService, a great datastore module that has already ironed all of these issues, with autosave and saving on bindtoclose.