I already have the necessary/working code. Please don’t reply with code
The database is to check if there is any ongoing savings, if yes it kicks the player when the leave and join again. (with warning data saving in progress)
It goes like this
when player enters the database is accessed and the value is set to true
when player leaves, after saving data or the maximum amount of tries exceeds the value is set to false(which means the saving is done!)
So here’s the question:
Is there any problem with this method?
Is there any alternative?
Before you save, set the value to true, then wrap the saving in a pcall and get its success value and set the value back to false depending on the success value
local savingInProgress
local success = nil
local errorMsg = nil
local attempt = 1
savingInProgress = true --currently saving
repeat
success, errorMsg = pcall(function()
return datastore:SetAsync(plr.UserId, data) --saving in action
end)
attempt += 1 --saving attempt increased by 1
if not success then --if saving is failed
warn(errorMsg )
task.wait(3) --wait 3 seconds before starting again
end
until success or attempt == 5 --keep trying to save until it's successful or you've already tried 5 times
savingInProgress = false --saving is done!
Edit: You may also want to use UpdateAsync depending on what you are saving:
In this case then I have no idea, but I don’t think the player could rejoin in such a short interval that the data from previous session is still saving
prob needa create some sort of warning system for urself (or maybe just use analytics page) to make sure ur never actually hitting that limit. atleast throw an error if max tries is reached and data isnt saved.
make sure u have checks for:
if the player rejoins right after leaving.
if the player quits before their data is even loaded (playerRemoving will fire before any data is even seen, could be a memory leak)
if the server is shutdown then make sure to save
some smart queue system for processing saves n whatnot. making sure only to save the next one if ur not about to hit the limit.