How to save tables in datastore

lets say i created a table in a datastore like this

local Player = {
     ["Money"] = {
          ["Bank"] = 0,
          ["Wallet"] = 0,
     },
     ["Tools"] = {
          ["ExampleTool"] = 0,
          ["ExampleTool1"] = 0,
          ["ExampleTool2"] = 0
     },
     ["Vehicles"] = {
          ["ExampleVehicle"] = 0,
          ["ExampleVehicle1"] = 0,
          ["ExampleVehicle2"] = 0
     }
}

and i wanted to save the datastore but i only wanted to save lets say the [“Tools”], how would i go about doing that (( please note i cant just rewrite the whole array due to the actual array size im using

setasync(thetable["Tools"]) -- easy

To save only the tools, you can do setasync(thetable["Tools"]) like @commitblue said, but I want you to know that tables can be saved into a data store by associating them with a specific key within the data store.

local Inventory = { -- Example Player Inventory
   ["Item"] = 101,
]

game:GetService("Players").PlayerRemoving:Connect(function(Player) -- Checking if player is leaving game
   local Success, Error = pcall(function()
      return game:GetService("DataStoreService"):GetDataStore("DATA_STORE_NAME_HERE"):SetAsync(Player.UserId.."Inventory", Inventory)
      -- Setting the data for inventory for the player
   end)
   if Error then
      warn(Error) -- Showing there was error (Can also keep in log to fix)
   end
end)

This is a quick example, but the important thing to notice is that tables can be saved as keys in a data store. In order to avoid data loss, you should use:UdateAsync() instead of:SetAsync() to set the data in an optimal approach. I hope this helped.

1 Like