Help with Datastores

I am trying to use a datastore to save information however, it needs to be able to be grouped for instance.

For instance, three types of warning C1, C2, and C3. But I need to do it in a way that when I pull the data from the data store I can separate them, so that I have the reasons and also amount of each warning.

For example.
C1 - Glitching
C1 - Being an idiot.

There are 2 C1s, and I need to also be able to pull the reasons so that I can display it for example, C1 for Glitching.

Sorry if it was a bit wordy.

I see your problem, but can I see the script so I can better understand what’s going on so I can help? :sweat_smile:

You can save it all under 1 table as either a nested dictionary or array, although personally I’d prefer the dictionary option. It should look something like so:

{
  "C1" = {"Being an idiot", "Glitching"},
  "C1" = { -- Another example, similar to json.
         {"Reason" = "Being an idiot",
          "Date given" = 10921},
          
         {"Reason" = "Glitching",
         "Date given" = 101521}
         }
  "C2" = {},
  "C3" = {},
}

Another possibility is using Scope for the GlobalDataStore.

You could have:

local Warnings1 = DataStore:GetGlobalDataStore("Warnings", "C1")
local Warnings2 = DataStore:GetGlobalDataStore("Warnings", "C2")
local Warnings3 = DataStore:GetGlobalDataStore("Warnings", "C3")

however I dont recommend it, as that would be 3 datastore calls versus just 1 if you were to instead use a dictionary like suggested above. I am only posting this as a method example.

However I wouldn’t use an array like suggested above. I would stick to a full dictionary layout, like so:

local warnings = {
	C1 = {
		TimeStampAsString = "Reason",
		TimeStampAsString = "Reason",
		TimeStampAsString = "Reason",
	},
	C2 = {
		TimeStampAsString = "Reason",
		TimeStampAsString = "Reason",
	},
	C3 = {
		TimeStampAsString = "Reason",
	},
}

-- usage example:
for timestamp, reason in pairs(warnings.C1) do
	print("C1 Warning:", reason, "(", timestamp, ")")
end

Ok, so with that, I am guessing the timestamp would return the time of it also with the list what’s the most effective way to get the amount of warnings. E.G amount of C1s.