I have written this piece of code to transfer data from a old data store to profile store and I need some feedback / advice on what needs to improved and some suggestions if there are any.
local function LoadOldData(Player, Profile)
if Services.RunService:IsStudio() then return end
local Key = `Player_{Player.UserId}`
if Profile.SessionLoadCount > 1 then return false end
local Success, Result = pcall(function()
return Misc.OldDataStore:GetAsync(Key)
end)
if not Success then return "Error" end
if not Result then
return false
else
-- Move data
end
end
local function retry(timeMultiplier, callback, ...)
local attempts = -1
local results = {}
while (not results[1]) do
results = {pcall(callback, ...)}
attempts += 1
task.wait(attempts^2 * timeMultiplier)
end
return select(2, table.unpack(results))
end
(taken from my hardcore desert survival game neniadea)
my bad i ain mean to give u life advice
you dont want the risk of data loss so keep trying til its successful with a loop
function above is called like
local result = retry(0.05, Misc.OldDataStore.GetAsync, Misc.OldDataStore, Key)
you can also restructure it so that it includes success
local function retry(timeMultiplier, callback, ...)
local attempts = -1
local results = {}
while (not results[1]) do
results = {pcall(callback, ...)}
attempts += 1
task.wait(attempts^2 * timeMultiplier)
end
return table.unpack(results)
end
called in the same way
you also dont need a quadratic waiting period you can just make it linear (attempts * timeMultiplier) or even constant (1)
use desmos and see what works the best
also as a few sidenotes: use the player user id for the key (you dont need Player_), kick the player if its not successful, variabilize services.runserivce:IsStudio() and rename the function to canLoadPastData or something- it returns a boolean so signify that in the function name