How to update data that already exists in a Datastore?

Hi all.

This problem is probably a simple issue, but I can’t figure it out. Essentially

Table1 = {
["Apple"] = 2,
["Orange"] = 3,
["Banana"] = 4
}

How would I make one of those values, using "Banana" in this example, to be incremented by 1, **without** the rest of the saved values being affected? Essentially, let's say there are two currencies, one named Coins and one named Gems. They are both saved in the same database in the same table. How would you increase coins by 1 without changing or affecting the values of Gems?


I really appreciate any help you can provide, thanks in advance.

Standard approach needs you to use UpdateAsync to execute the update. The existing data will be updated to the new one.

That’s what i’ve been thinking, but wouldn’t some values just zero out? Like, if you want to

["Apple"] = 2
["Banana"] = 4

wouldn't just saying
["Banana"] = 6 
cancel out the other values in the table?

It depends on how you use the method. Either you use the old data from previously, or you use the current data and overwrite it with the new values.

Try checking out any posts or topics related to UpdateAsync for further insight, or consult the documentation page about it.

You can create a new table but only change the value of Banana and use the UpdateAsync function to update the datastore with the newly created table

local fruitData = get the old table from the datastore before updating

local newData = {
  ["Apple"] = fruitData["Apple"], -- keep the values you don't want to change the same as the old data
  ["Orange"] = fruitData["Orange"],
  ["Banana"] = fruitData["Banana"] + incrementamount -- this increases the existing amount by however much provided
}

update the datastore with the newData table

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.