I have a working datastore, but my game is a single player game which means if saving fails it can’t repeat trying to save due to the server shutting down. I’ve seen datastore 2 but i really don’t understand it. Could anyone kink a good source please?
I don’t feel like DataStore2 or ProfileService is necessary most of the time. It’s probably better to just use regular DataStoreService and just code it well. I’m not an authority on data stores, so you can take this with a grain of salt.
Well, if you are afraid of it failing, you can just make a loop with a pcall function. If the saving fails, try to save again until it has exhausted its tries (say like 15). If your code is good, you probably won’t encounter anything related to losing data.
I have a repeat until but the server will shutdown.
Actually there’s a model I’ll try to implement it into my code. Better safe than sorry.
You can use game:BindToClose() to save it when the server shuts down. Also there is a bit of a buffer, so saving won’t be an issue. If you code it well, it won’t break. Check out this: Datastore Tutorial for Beginners
Another good way is have default data values, and make it so it doesn’t accidentally overwrite the player’s data with default data (this might happen if you save their data before its loaded, if you have a global autosave)
Don’t just rely on DataStore2 or ProfileService to do everything for you, it’s not a good habit.
If you are scared that it might overwrite their data, make an attribute in your leaderstats folder (or wherever you want) and when the data is loaded, set the attribute to true. Then, when saving, check if the data is loaded. If not, then don’t save. However, it is highly unlikely that it will override their data if you are careful/
Yeah, but what I was thinking is just check if their data is the set defaults, and it won’t save their data if it is. Obviously if they don’t play the game (so their data isn’t modified) then there is also no reason to save it if they literally didn’t play at all. This could be helpful for the DataStoreService cooldown (or whatever you call it).
If I’m spreading misinformation, sorry about that. I’m not an authority on data stores.
It wouldn’t be that bad. Again, if you are scared its gonna happen (its probably not unless your code is trash), just use a loop that tries to save multiple times. DataStoreService cooldown normally only triggers if you try to save REALLY fast.
You mean by loop code,
local success
repeat
success = pcall(function() YOURSAVECODEHERE end) task.wait()
until success
Like that?
No., you use a for loop. If the data is corrupted it will not save the other players’ data. Set a variable to attempts, and try to save it:
Edit: Sorry for the terrible indentation, devforum is weird with it.
local attempts = 15 -- random number
for i = 1, attempts do
local success = pcall(function()
playdata:SetAsync(player.UserId, data)
end)
if success then
break
end
task.wait()
end
I have a part of my script which checks if the data is nil if i remember correctly.
Thats not the only reason your data may fail to save.
local attempts = 15
for i = 1, attempts do
local success = pcall(function()
playdata:SetAsync(player.UserId, data)
end
if success then
break -- add this so it ends the loop if successful
end
end
That line I added would make the code better I think…
I know. But because all of the values are 0 then it won’t save if it didn’t load.
Check post 8. I explain how to avoid it if you are really scared its going to break
Have a default value preset and check if its the default before saving. As long as its anything but the default, it should be safe to save.
If they don’t play the game it wont save. IDK if it is going to cause any issues but using what I said in post 8 is probably better. The problem could easily be avoided if your code is good.
I’ll attempt to add it to my code. In case if it doesn’t for some reason work with all of these safety things, i have 1 trick up my sleeve: W E E B H O O K. sends me data and player name. It’s probably not needed if i use BindToClose and the attribute thing.