Is it possible to save a table when a new key and value is added with DS2?

Edit

Thanks to XxELECTROFUSIONxX
for DMing me and helping me out with the
DS2.

So I’m not sure how I would save a new key and value inside a Table

I’ve tried this out, but there’s not much happening and I only know how to save Integers, Bools, and Strings with DS2.

local LevelService = {Client = {}}
local LevelData = {}

local ServerScriptService = game:GetService("ServerScriptService")

local DataStore2 = require(ServerScriptService.DataStore2)

local Players = game:GetService("Players")
local Obbys = workspace:WaitForChild("Obbys")


function LevelService:GetLevelData(player) 
    local ID = tostring(player.UserId)

    return LevelData[ID]
end

function LevelService:Start()
	Players.PlayerAdded:Connect(function(player)
        local ID = tostring(player.UserId)
        local LevelDataStore = DataStore2("Levels", player)

        if not(LevelData[ID]) then
            LevelData[ID] = {
                ["Easy"] = {
                    ["Level 1"] = Obbys.Easy["Level 1"].Checkpoints["1"].PrimaryPart
                }
            }

        end
        LevelDataStore:OnUpdate(function(updatedLevel)
            local levelTable = LevelDataStore:GetTable(updatedLevel)
            for i,v in pairs(levelTable) do
                print(i)
                print(v)
            end
        end)
    end)

    Players.PlayerRemoving:Connect(function(player)
        local ID = tostring(player.UserId)

        if(LevelData[ID]) then
            LevelData[ID] = nil
        end
    end)
end

return LevelService

inside another script, I’m doing

if not(LevelData[Diffuclty][NextLevel.Value.Name]) then
   LevelData[Diffuclty][NextLevel.Value.Name] = Obbys[Diffuclty][NextLevel.Value.Name].Checkpoints["1"].PrimaryPart
else
   print("Level already finished once")
end

to add the new Level when you finished the first Stage or maybe the Second depends on which stage you are on

and I was also wondering, is it even possible to save something like this?

        local function TableUpdate(updatedLevel)	
            local levelTable = LevelDataStore:GetTable(updatedLevel)
            for i,v in pairs(levelTable) do
                print(i)
                print(v)
            end
        end

        if not(LevelData[ID]) then
            LevelData[ID] = {
                ["Easy"] = {
                    ["Level 1"] = Obbys.Easy["Level 1"].Checkpoints["1"].PrimaryPart
                }
            }
            TableUpdate(LevelData[ID]) 
            LevelDataStore:OnUpdate(TableUpdate) 
        end

so I’ve done this, and it prints out key and value now, but not sure how I would save it still.

If you’re trying to save a table, try saving this to the datastore: HttpService:JSONEncode({table stuff here})
When you’re getting data, use HttpService:JSONDecode(datastore:GetAsync()) and it will return the table.
I never used Datastore2, so I apologise if I didn’t say things the correct way. I’m sure HttpService stuff I mentioned is right. I use this when saving tables.

HttpService:JSONEncode(…)

Do not encode data for the purpose of saving, as it is encoded internally to a JSON string when saving to Roblox’s DataStores. The DS2 wrapper internally uses Roblox’s DataStoreService too.

Doing this just adds unnecessary characters to the string that’s saved, taking up more storage.


To address the post:

There’s a chance Diffuclty might be a typo though I doubt that’s the case given you’ve typed that 3 places.

I don’t see you saving using DataStore2 anywhere in the scripts provided, saving a table should as easy as saving any other value. Just pass a table instead.
From what I see, I also feel like you’re only modifying the table result returned that was never saved instead of saving it for later to the DataStore.

1 Like

This is inside my StatsService Script

DataStore2.Combine("Test-Data2", "Opals", "Levels")

I’ve done this inside my LevelService Script

       Players.PlayerAdded:Connect(function(player)

        local ID = tostring(player.UserId)

        local LevelStore = DataStore2("Levels", player)

        

        if not(LevelData[ID]) then

            LevelData[ID] = {

                ["Easy"] = {

                    ["Level 1"] = Obbys.Easy["Level 1"].Checkpoints["1"].PrimaryPart

                }

            }

            LevelStore:OnUpdate(function(newLevelData)

                LevelData[ID] = newLevelData

            end)

        end

    end)

and inside my CheckpointService I’m not sure what to do here

                local LevelsStore = DataStore2("Levels", player)
                if not(LevelData[Diffuclty][NextLevel.Value.Name]) then
                    LevelData[Diffuclty][NextLevel.Value.Name] = Obbys[Diffuclty][NextLevel.Value.Name].Checkpoints["1"].PrimaryPart   
                    -- What do I do here?
                else
                    print("Level already finished once")
                end