I am a new developer and want to make sure that my data stores will save data every time without fail. Right now I just use the basic form of wrapping my SetAsync and GetAsync in a pcall for when the player leaves and joins. My main question is, is this enough to keep critical player data stored or are there important steps I am missing?
It also matters how frequently and/or when you trigger saves
Oh so I should save more often than just when the player leaves?
You can never be 100% safe from data loss. When the last player in a server leaves you have around 30 seconds to save their data before the server shuts down. In the case that roblox datastore servers are completly down in that timeframe theres nothing you can do and the players data will be lost.
You’re on the right track with using pcalls and retrying when it fails. Incase you havent already you should make the time it waits before retrying after a fail increase each time (first fail waits 1 second, second waits 2, third 4, fourth 8, etc)
You could add session locking to ensure that a players data is saved before they rejoin another server but it isnt neccessary.
If you want to do autosaves, you should only trigger them every 3-5 minutes to prevent data loss due to using up all your quota. (quota = the amount of reads and writes you’re allowed to do in a set amount of time)
local TimeFrame = 1
local function SaveData ()
local success, errorMessage = pcall(function()
SkillTreeDataStore:SetAsync(player.UserId, SaveTable)
end)
return success
end
while true do
local result = SaveData()
if result or TimeFrame == 32 then
break
end
task.wait(TimeFrame)
TimeFrame *= 2
end
Would something like this be good for when the player leaves the game?
No. SetAsync is last-write-wins and can drop saves. Use UpdateAsync with a per-player lock, snapshot, bounded backoff, jitter, and handle PlayerRemoving + BindToClose.
not quite. From what i can see the while true loop runs on game startup, not when you want to save data specifically. What i originally meant was have a loop that runs every 5 minutes that saves data, and in the SaveData function youll repeat the save with increasing cooldown (1, 2, 4, 8 ,16) up to 5 times or the save succeeds.
You also want to trigger the SaveData function when a player leaves and game:BindToClose()
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.