Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to ensure the DataStore is properly Loading Saved Data?

Asked by 3 months ago
Edited 3 months ago

Hello all,

I have a DataStore for storing my BoolValues. The BoolValues are nested underneath this ServerScript. My DataStore should do the following: 1. Create new Data for the player upon the first time they enter. 2. Load the Saved Data for returning players. 3. Save Data.

My DataStore successfully creates new data, and saves data. But its creating new data each time the player enters the game. Can't have this. x_x I've marked what each function is suppose to do in the script below, and I've also marked where the errors appears to be happening:

 function dataStoreAttempt(dataStoreFunction)

and possibly: function loadData(Player)

I've added Pcalls that were not there before, and I'm not entirely sure if I'm using GetAsync and UpdateAsync properly.

Here is my code:

    local module = {}
    local DataStoreService = game:GetService("DataStoreService")
    local InventoryItems = DataStoreService:GetDataStore("XXXX")
    local ToolGiverCheck = require(script.ToolGiverCheck) -- This is my script nested under this one that provides all the BoolValues.
    datastore_success = {}
    hasValues = require(script.HasValues)

    AUTOSAVE_INTERVAL = 120
    DATASTORE_RETRIES = 3
    Creating the Table for the DataStore to draw from, where my BoolValues are housed:

    function createData()
        local data = {}
        data.tbl_1 = {}
        data.tbl_2 = {}
        for x = 1, #hasValues do
            table.insert(data.tbl_1, hasValues[x])
            data[hasValues[x]] = false
        end
        return data
    end
    --First attempt to check the Data when the Player arrives, which is where the error seems to be:

    function dataStoreAttempt(dataStoreFunction)
        local tries = 0
        local success = false
        local data = nil
        repeat
            tries = tries + 1
            success = pcall(function() data = dataStoreFunction() end)
            if not success then
     wait(1) 
    print('ERROR AT ATTEMPT TO RECALL')
     end
            until tries == DATASTORE_RETRIES or success
            return success, data
        end
    --Where the Data is Loaded for the Player:

       function loadData(Player)
            local Key = "PlayerIdNumber-"..Player.UserId.."_ToolItemsXXXX"
            local InvData = Player:WaitForChild("Inventory")
            local  success, err = pcall(function() data = dataStoreAttempt() end)
            dataStoreAttempt(function()
                 return InventoryItems:GetAsync(Key)
            end)
            if success then
                if not data then
                    data = createData()
                    print("Inventory new data successfully created for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
                else
                    for x = 1, #data.tbl_1 do
                        local value = data.tbl_1[x]
                        local inv_value = InvData:WaitForChild(value)
                        inv_value.Value = data.tbl_2[value]
                    end

                    print("Inventory data successfully loaded for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
                end
                datastore_success["id_"..Player.UserId] = true
            else
                print("Inventory data FAILED to load for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
            end
        end
    --Where the Data saves for the Player, this appears to be working:

    function saveData(Player)
        if datastore_success["id_"..Player.UserId] then
            local Key = "PlayerIdNumber-"..Player.UserId.."_ToolItemsXXXX"
            local InvData = Player:WaitForChild("Inventory")
            local data = createData()
            for x = 1, #data.tbl_1 do
                local value = data.tbl_1[x]
                local inv_value = InvData:WaitForChild(value)
                data.tbl_2[value] = inv_value.Value
            end

        local  success = pcall(function() data = dataStoreAttempt() end)
        dataStoreAttempt(function()
             return InventoryItems:UpdateAsync(Key,function(data)


            if not success then
                print("Inventory data FAILED to save for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
            else
                print("Inventory data successfully saved for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
            end
        end)
    end)
    end
    end
    --The Player Connecting/Removing/Spawning, this all appears to have been working fine too:

    game.Players.PlayerAdded:Connect(function(Player) -- Inventory
        local tnew = Instance.new
        local ts = tnew("Folder")
        ts.Name = "Inventory"
        ts.Parent = Player
        for x = 1, #hasValues do
            local tool = tnew("BoolValue")
            tool.Name = hasValues[x]
            tool.Value = false
            tool.Parent = ts
        end
        loadData(Player)
        repeat wait() until Player.Character
    end)

    game.Players.PlayerRemoving:Connect(function(Player)
        saveData(Player)
        datastore_success["id_"..Player.UserId] = nil
    end)

    spawn(function()
        while wait(AUTOSAVE_INTERVAL) do
            for i, Player in pairs (game:GetService("Players"):GetChildren()) do
                print("Inventory data Auto-saving for " .. Player.Name .. " .")
                saveData(Player) 
            end  
        end  
    end) 
 return module

How datastoredFunction() use to appear, before I started messing with the Pcalls, in response to the comment below:

function dataStoreAttempt(dataStoreFunction)
    local tries = 0
    local success = false
    local data = nil
    repeat
        tries = tries + 1
        success = pcall(function() data = dataStoreFunction() end)
        if not success then wait(1) end
    until tries == DATASTORE_RETRIES or success
    return success, data
end
function loadData(Player)
    local Key = "PlayerIdNum-"..Player.UserId.."_ToolItemsForMW1928374123123"
    local InvData = Player:WaitForChild("Inventory")
    local success, data = dataStoreAttempt(function()
         return InventoryItems:GetAsync(Key)
    end)
    if success then
        if not data then
            data = createData()
            print("Inventory new data successfully created for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
        else
            for x = 1, #data.tbl_1 do
                local value = data.tbl_1[x]
                local inv_value = InvData:WaitForChild(value)
                inv_value.Value = data.tbl_2[value]
            end

            print("Inventory data successfully loaded for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
        end
        datastore_success["id_"..Player.UserId] = true
    else
        print("Inventory data FAILED to load for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
    end
end

Any assistance here is very much appreciated. Thank you. :)

0
Line 43, you're not passing any args to dataStoreAttempt, was this intentional? SteamG0D 579 — 3mo
0
It is, I've edited the main post to show how it use to appear before recent edits. Never2Humble 76 — 3mo
0
Also have you been trying to test this inside the studio? If so, have you enabled the studio's access to the data stores? SteamG0D 579 — 3mo
0
In your original script, everything seems to be just fine, you shouldn't need to mess with pcalls there since you have it in your dataStoreAttempt function. SteamG0D 579 — 3mo
View all comments (2 more)
0
The only thing I suggest you do is print success and then print data on two separate lines just before line 18, with the old script, just to make sure the data really is nil and not 0 or false or something like that. SteamG0D 579 — 3mo
0
Sorry for the spam, but can I see your original script for saving data? (I'd like to fix your original script because it your most recent one seems like you were just trying everything to fix it, so it's a bit cluttered) From what I can see I think the problem might be that the keys are not pointing to the same thing because other than that I don't know what would cause your saving/loading functio SteamG0D 579 — 3mo

Answer this question