I am making a DataStore script, and I am having some tricky issue here. My script (as you can see below) over-writes ALL the data in the DataStore. But I only want it to ONLY over-write the money key in the table saved there; as well as to leave the other keys in the table alone. (There are other table-keys in the DataStore but i did not show them in the script) Any help/suggestions would be gladly appreciated.
P.S. I have heard about UpdateAsync() but I cannot fully comprehend how it works. Maybe it will help, I dont really know.
If I presented this too confusingly, I would be glad to clarify it further in the comments if you wish.
local DataStoreService = game:GetService("DataStoreService")
local userDataStore = DataStoreService:GetDataStore("UserDataStore")
-- Default data
local starterData = {
money = 100
}
-- Save
function save(player)
local success, err = pcall(function()
-- THE PART I AM HAVING TROUBLE WITH, i only want it to overwrite the "money" key in the table below
userDataStore:SetAsync(tostring("Player_" .. player.UserId),
{
money = game.Workspace.PlayerStats["Player_" .. player.UserId].NoobMarks.Value
})
end)
if err then print("Player data failed to save for " .. player.Name) end
end
-- Load
function load(player)
local playerStats = game:GetService("ReplicatedStorage"):WaitForChild("Templates"):WaitForChild("PlayerStatsTemplate"):Clone()
playerStats.Name = "Player_" .. player.UserId
playerStats.Parent = game.Workspace.PlayerStats
local data
local success = pcall(function()
-- Set money
data = userDataStore:GetAsync(tostring("Player_" .. player.UserId))
player:WaitForChild("leaderstats"):WaitForChild("NoobMarks").Value = data.money or starterData.money
end)
-- Load data
if success then
playerStats.NoobMarks.Value = data.money or starterData.money
else
print("Player data failed to load for " .. player.Name)
end
end
game.Players.PlayerAdded:Connect(load)
game.Players.PlayerRemoving:Connect(save)
Thanks in advance.
