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)