#Is this a valid data loading/saving system?

1 messages · Page 1 of 1 (latest)

rugged crescent
#
local Datastore = game:GetService("DataStoreService")
local PlayerInventory = Datastore:GetDataStore("PlayerInventory")
local PlayerPowerds = Datastore:GetDataStore("PlayerInventory", "Powerds")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local powerds = Instance.new("IntValue")
    powerds.Name = "Powerds"
    powerds.Parent = leaderstats
    
    --loading data
    local success, CurrentPowerds = pcall(function()
        return PlayerPowerds:GetAsync(plr.UserId)
    end)
    if success then
        if CurrentPowerds ~= nil then
            print("Data loaded")
            print(CurrentPowerds)
            powerds.Value = CurrentPowerds
        end
    else
        warn("Failed to find data, using default data")
        powerds.Value = 10
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local powerds = plr.leaderstats.Powerds
    local success = false
    local tries = 0
    while tries < 4 do
        success = pcall(function()
            PlayerPowerds:SetAsync(plr.UserId, powerds.Value)
        end)
        if success then
            print("Data saved")
            break
        else
            print("Save unsuccessfull, trying agian")
            tries += 1
            wait(0.3)
        end
    end
end)
south sun
#

does it work?

plucky forge
#

If it works, then it’s valid, if it doesn’t work, then it’s not. If you’re asking if it’s safe, then I’d say it’s probably not. I’d recommend using profilestore.

rugged crescent
spiral scaffold
# rugged crescent ```lua local Datastore = game:GetService("DataStoreService") local PlayerInvento...

If the datastore request fails your code will give them a default value that will overwrite their real data when they leave, leading to data loss. A better option is to retry until you get success or kick the player

If the player leaves before their data is fully loaded, the game will save the default value of Powerds to their data, leading to data loss. You should have a "loaded" flag that defaults to false and gets enabled after all of their data is fully loaded. When the player leaves, only save the data if their loaded flag is true

I'd recommend waiting more than 0.3 seconds between save attempts

Its a good idea to progressively autosave the player's data every duration of time (ex every 30 seconds or every 5 minutes). This means if roblox goes down and the player leaves, they will only lose a short amount of progress instead of their whole play session

If the player leaves the game and joins back (often easy to reproduce by pressing the play button on the website while still ingame) before their data saves, the game will load an outdated version of their data, leading to data loss. You should implement a session locking system to fix this

These problems all have varying levels of difficulty to fix. I manually did all of these and it was tedious to get right. I'd recommend profile service because it handles all of these problems for you:
https://devforum.roblox.com/t/profilestore-save-your-player-data-easy-datastore-module/3190543