Access a value inside the DataStore Table

Hello, how do you access a value from a datastore table. I don’t want to SetAsync the whole datastore table everytime a player buys something from the shop. The way the shop works is when the player bought something, it will update the value of that item in the datastore. However, in my current script, it has to make a whole new table and updates it entirely everytime they bought something. I just want it that when they bought something, the script will access the datastore table and change the value of that item alone. My script looks like this atm. Tysm!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService('DataStoreService')
local playeritems2 = DataStoreService:GetDataStore('PlayerItems')
local RunService = game:GetService("RunService")
local BuyItem = game.ReplicatedStorage.BuyItem

local function create_table(player,item)
	local player_items = {}

	for _,item in pairs(player.Items:GetChildren()) do
		player_items[item.Name] = item.Value
	end
	return player_items
end

local function BuyThisItem(player, price, item)
	if player.leaderstats.Points.Value >= price then
		player.leaderstats.Points.Value  = player.leaderstats.Points.Value - price
		player.Items[item].Value = true
		
		
		local player_items = create_table(player)
		local success, err = pcall(function()
			local playerUserId = 'Player_'..player.UserId
			playeritems2:SetAsync(playerUserId, player_items)
		end)
		print("Datastore Update: "..item)
		
	end
end


BuyItem.OnServerEvent:Connect(BuyThisItem)

Short answer: Don’t.
Slightly longer answer: What you’re doing here is extremely wasteful, as you should only be saving player data when:

  1. Autosaving (saving every 30 or 60 seconds)
  2. They are leaving the game
  3. The server is shutting down

I’m doing this because a few people experience that their data is getting wiped out. So I decided that when they buy something, the game will update their datastore right away.

Yeah, this is wasteful so I’m asking for help if there’s a way to access single value inside a datastore table to reduce the amount of stress in the game.

Then just call SetAsync() for the one player when they purchase something as opposed to every player in the server one just one player purchases something.

Then you’re doing datastore wrong. If you use it properly, you won’t have these issues. I suggest you post that datastoring code in #help-and-feedback:code-review.