Singular datavalue does not save

Ya so I am at a complete loss on why this is not working correctly. Every other data value saves accordingly but for some reason the hotbarslot values do not. Here is my function that actually saves the players data:

function DataStoreHandle.SaveDataToSlot(Player, Data, SaveSlot) 
    local PlayerDataStore, ChosenSlot = RetrieveSaveSlot(Player, SaveSlot) --Retreiving specific slot datastore

    local SessionLockStatus = VerifySessionLock(Player) --Whenever editing data, it is crucial to verify the players session lock for safety 

    --Of course if the session is valid we proceed and save their data, but if not we hand it off to InvalidSession and let it take the appropriate action
    if SessionLockStatus == "Valid" then 

        local success, errorMessage = pcall(function()
            PlayerDataStore:SetAsync(ChosenSlot, Data)
        end)

        if success then
            if DebugMode then print("Successfully saved data",Data,"to "..SaveSlot) end
            return true
        else        
            if DebugMode then warn("Could not save"..SaveSlot) end
            return false
        end

    else
        DataStoreHandle.InvalidSession(Player, SessionLockStatus) --If we cannot verify the players session, then we call InvalidSession
    end

end

Output from when making a change/editing the hotbarslot data:

  00:02:54.748  Disconnect from 127.0.0.1|55521  -  Studio
  00:02:54.769  Successfully retrieved the data store for PlayerData_1594902353_Slot1  -  Server - DataStoreHandle:77
  00:02:54.849  Session lock valid for player:  FusionMasterGames  -  Server - DataStoreHandle:58
  00:02:55.100  Successfully saved data  â–Ľ  {
                    ["Currency"] = 1782,
                    ["CurrentXP"] = 0,
                    ["EquippedItems"] = {},
                    ["HotBarItems"] =  â–Ľ  {
                       [1] = "Apple",
                       [2] = "IronSword",
                       [3] = "Bread",
                       [8] = "HumanEye"
                    },
                    ["Inventory"] =  â–¶ {...},
                    ["Level"] = 1,
                    ["Reputation"] =  â–¶ {...},
                    ["SlotNum"] = "Slot1",
                    ["UserId"] = 1594902353,
                    ["XPtoNextLevel"] = 50
                 } to Slot1  -  Server - DataStoreHandle:193
  00:02:55.100  Player data removed for:FusionMasterGames  -  Server - PlayerData:309
  00:02:55.249  Session lock successfully removed for player: FusionMasterGames  -  Server - DataStoreHandle:111

Output when loading data on player join:

  23:56:09.399   â–Ľ  {
                    ["Currency"] = 1782,
                    ["CurrentXP"] = 0,
                    ["EquippedItems"] = {},
                    ["HotBarItems"] =  â–Ľ  {
                       [1] = "Apple",
                       [2] = "IronSword",
                       [3] = "Bread"
                    },
                    ["Inventory"] =  â–¶ {...},
                    ["Level"] = 1,
                    ["Reputation"] =  â–¶ {...},
                    ["SlotNum"] = "Slot1",
                    ["UserId"] = 1594902353,
                    ["XPtoNextLevel"] = 50
                 }  -  Server - StartUpProcedures:57

The output pasted first is the players data table printed right from the save function itself and you can see that I put a “HumanEye” item in slot 8.

Then, in the output pasted last is when I load back into the game and as you can see it didnt persist across the sessions…there is nothing in slot 8.

I just dont understand why this is the case as my save function literally saved the table with those values in it and when any other value is changed it saves that value…why did it not persist???

Hello,
So here is quite similar issue to yours and reason behind it: Data Store "stringifies" array indexes if array doesn't start from index 1, and behaves incosistently when array has missing indexes - #10 by tnavarts
In short: Datastore doesn’t support nil values in array, and cuts off the array after first encounter. You can fix this by filling empty values with nothing, like empty string ("") or some placeholder value.

{
    [1] = "Test1",
    [2] = "Test2",
    [3] = "",
    [4] = "Test3",
    ....
}

Ohhhh shoot I completely forgot about unordered arrays! Thanks!

1 Like