So I have a database to check datasavings, is there any problem with this method?

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:

1 Like

sorry, i don’t need code?
i just want to know the answer for the 2 questions
thank you for your effort by the way

Oops, I don’t know what I was thinking

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

1 Like

Never mind, it seems like it can happen, but it also looks like other games have implemented the same method:

I don’t think there is a problem with that as long as the player’s progress is not wiped and that they are informed of what’s happening

1 Like

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.
1 Like

nice suggestions, this helped a lot thanks

np, if u have any other questions about saving data lmk.

it interests me a ton!

1 Like