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.
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
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