Datastore Dictionaries

Heya! o/ Thank you for reading!

Just a quick question. If I want to Update a value inside a DataStore, and the DataStore is made of a MainArray containing 2 Dictionaries. When using UpdateAsync Im getting the whole MainArray, and after updating the Key I want I should Store the whole MainArray again. Isn’t that wrong?.. isnt that too much data been sent over and over again, being unnecesary (specially if the dictionaries grows over time)?.. Is there a way to not get the whole MainArray and just get a specific Dictionary Key when query the DataStore? And update just that specific key?

2 Dictionaries inside the MainArray (this is just a quick example). And Storing that array into the Player’s DataStore

local DSS = game:GetService("DataStoreService")
local DATA = DSS:GetDataStore("Data")

local a = {}
a[1534538] = {Stamp = 21, By = 34585, R = "bla bla"}
a[4156468] = {Stamp = 50, By = 48432, R = "bla bla 2"}

local b = {}
b[4564646] = {Stamp = 86, By = 42123, R = "bla bla"}
b[1678676] = {Stamp = 45, By = 96411, R = "bla bla 2"}

local MainArray = {a,b}

DATA:SetAsync(PlayerID, MainArray)

When I try to update that information, Im using UpdateAsync(). (Brief dummy example)

DATA:UpdateAsync(PlayerID, function(array)
	local newArray = array
-- Accessing the Item b in the array, accessing the Dictionary key, then the value
	newArray[2]["4564646"]["Stamp"] = 8000
-- Returning the whole array again to be stored...
	return newArray

Thank you for your time c:

Why are there two tables, A and B, with more data in them?
(datastore have 4,000,000 byte per key limitation)

Cause Im testing storing more data on each DataStore entry. But thats not relevant…
Im just asking if its possible to access a Key inside the datastore, and dont get the whole DataStore.
BTW, after many tests Im kinda sure its not possible to access Keys inside a dataStore, you get the whole or nothing…

1 Like

You are right, that’s how it works. What you have is a rigid data model and as all database transactions are atomic (meaning that something is done or nothing is done) it is mandatory to change a complete record (and not just a part of it).

The solution to this type of problem is to change the data model to a more flexible model. For example, separate the data most likely to change in its own datastore or something like that. If you want to go deeper you could read about the entity-relationship model or other ways of modeling data.

Anyway, keep this in mind:

  • In a roblox game, updating data frequently is a bad practice. Typically, when a player joins the game, you should load all his data from the datastore and only save his data when he leaves the game.
  • The data on the server (which has not yet been saved to the datastore) is in a safe space and will not change unexpectedly so there is no need to update it in the datastore until the player leaves the game.
  • UpdateAsync is typically used in exceptional cases (such as administrator commands) infrequently. That being the case, updating entire records (however complex they may be) does not represent a significant cost.

I may be wrong as I am still learning myself. I hope I have been helpful.

1 Like

You always reply very clear, smart and deep information. Thank you so much!!!

Yup, I was trying to get a better understanding about how I could use a datastore, and test to store more data. Actually this test is for some admin functions that probably won’t run so bad even if I update the whole table… But now, I prefer to split those tables into separates datastores.

Thank you so much for the list of advices :3
Yup Im very aware of those points thank you c:

Off topic question… why the text you quoted from me, is shown in spanish? (my language btw, seems translated by google or some app)

I speak Spanish, I forgot to disable the browser translator.

1 Like