How to get one part of DataStore?

My game has a lot of data written to a single DataStore. It’s a table, and each item in that table has more tables.

My question is, how would I retrieve a single table from the list of tables? For example, a data table would look something like this:

data = {
    Items = {...};
    Pets = {...};
    Currency = {...}
}

What would be the best way to simply get, say, the “Pets” table? I could get all the data, and just use something like data.Pets, but I want to make this as efficient as possible.

Thank you!

As I know, there isn’t a way to directly fetch a nested table from a Datastore without first retrieving the entire data.

Your only way is to save that table in a separate Datastore.

1 Like

If it’s stored like this, you can’t.

Not really possible

Why don’t you get the whole data and just store it in a module for permanent use as long as the user is connected? That’s what most games do as far as I’m aware. Even if it were possible, doing repeated datastore calls to get individual tables would not be as optimized as 1 call to get the whole table and putting the whole table in a module

1 Like
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local PlayerData = require(game.ServerScriptService.PlayerData)
local DataStore = DataStoreService:GetDataStore("MyDataStore")

Players.PlayerAdded:Connect(Player: Player)
        success, UserData = pcall(function()
			return DataStore:GetAsync(player.UserId)
		end)
		if success then
			if UserData then
				  PlayerData[Player.UserId] = UserData
			else
				-- Player has no data
				-- Create it
			end
		end
end

Players.PlayerRemoving:Connect(Player: Player)
      PlayerData[Player.UserId] = nil
end

-- Use PlayerData[Player.UserId].Pets all the time, etc

I was hoping that I could get one part of the data to retrieve offline players’ data.

Also, I tried writing to a ModuleScript from a Script with the code you provided, but it didn’t work.