DataStoreService SetAsync() Add Onto Pre-Existing Data

Hi, I’m needing help on how to add onto pre-existing data using DataStoreService. I have had no issues previously using this method but because I’m splitting some values into []. This is the method I am currently using:

		if Store:GetAsync(suspect) then
			local Data = Store:GetAsync(suspect)
			local NewData = {
				["Arrest"] = {
					Officer = player.Name,
					Charges = charges,
					Fine = totalfine
				},
				table.concat(Data, ",")
			}
			Store:SetAsync(suspect, NewData)
		else
			Store:SetAsync(suspect, {
				["Arrest"] = {
					Officer = player.Name,
					Charges = charges,
					Fine = totalfine
				}
			})
		end

Basically it splits my data up using “,” and then I can add onto it but because I am using [] I think it breaks it or it doesn’t work? I’m not sure as I don’t understand the issue, thank you in advance!

5 Likes

I don’t believe that datastore can save tables, correct me if I’m wrong. I’d suggest encoding and decoding it using HttpService.

5 Likes

I previously used this method with a dealership system as when someone purchased a car it would add onto their previously owned cars and it worked perfectly fine:

if VehicleData then
	local Cars = {
	 vehiclename,
	 table.concat(VehicleData, ",")
	}
	VehicleStorage:SetAsync(player.UserId, Cars)
else
	local Cars = {
	 vehiclename,
	}
	VehicleStorage:SetAsync(player.UserId, Cars)
end

I’m thinking it’s because I’m using [] this time instead of the other way but I’m confused.

I would just use “Arrest” but that errors so I’m using [“Arrest”] instead, but I don’t know how to make it so I can add onto their data if they have previous arrests.

3 Likes

My whole idea is to SAVE their records, so I am looking for a way to SAVE their data so it can be used between different servers.

3 Likes

save data to table, when player leaves then save it, setAsync can be fired every 6 seconds per key, soo keep that in mind

2 Likes

That doesn’t really help unfortunately. I’m not making it SetAsync() when the player leaves. I’m doing it when a new Arrest has been created, this way other people can see what the player’s previous arrests are. I’m just needing help on working out how to save it to the DataStore. In this situation I am using Dictionaries [] to save the data and then I need to find out how to add onto their previous data if they have any.
Example:
If the Player does NOT have data:

local currentData = Store:GetAsync(player.Name)
local newData = {
 ["Arrest"] = {
  Officer = "OfficerName"
 }
}

If the player HAS data:

local currentData = Store:GetAsync(player.Name)
local newData = {
 ["Arrest"] = {
  Officer = "OfficerName"
 },
 table.conact(currentData, ",") -- but what would I change "," to as its a dictionary??
}

Hopefully this helps!

3 Likes

as i told, you should save on leave, but store arrest in module script, then when leaving, save player data and arrest value

2 Likes

But if I was to save on leave how would I detect if an arrest was made or not? I’m using RemoteEvents and when the Event is triggered it creates the arrest store on the player. I’m basically just needing help on what to change the table.conact(table, "THIS") to as it has previously worked when I was not using dictionaries, instead I just used it for a regular table as above with the dealership system.

3 Likes

But still, the best alternative is to use module script for that, you’ll store arrest there, and save it on leave, this way you can acces module script and check if playerA is on arrest list or not, and save it if you wan’t to make arrest last after leaving game

2 Likes

If I was to go forward with this, how would I add onto their other arrests? So I can save multiple.

3 Likes

then you should load data to module script on join, you could make that module PlayerA have:

local module = {
PlayerA = {
Arrested = true,
Arrests = {--[[Arrests]]}}
}
return module
2 Likes

Do you have a link to where people have used ModuleScripts for DataStoreService similar to my situation?

3 Likes

Sadly no, but module script is a table, a data storing methood, read about them, and they are not for DataStoreService

require(module) = table it’s the same, you can save table to data store, but module is only a methood to show it when player is inside game

2 Likes

I would still prefer using DataStoreService as I have used that before and I haven’t utilised ModuleScripts apart from doing Config/Setting files. I’m just wanting to figure out how to add new data to existing data to a players DataStore, which as I said has worked but now I am using dictionaries it doesn’t work or I assume works differently.

2 Likes

Meh, module scripts are the same as dictionaries, they storing tables with data like variables, functions ect. You could use them to store player data when playing and then save data to data store when leaving, to get old data, getAsync when join and then save it to module script, when leaving get data from moduleScript and save it as dictionary

1 Like

If you want to add to pre-existing data, try looking into UpdateAsync().

It reads before it writes, so it might be useful.

2 Likes

Update async is using to compare data, it’s the same as getAsync / setAsync both in the same function, FireMan tries to add new data to existing one

1 Like

Correct, previously I used the method of using table.conact(data, "what_do_i_put_here")? Basically I split the data it gets and then puts it back into a table after being split up with the new arrest and is then SetAsync’d.

2 Likes

why just don’t do:

local array = {["A"] = {},["B"]={}} -- empty table, only for showcase

array["C"] = {} -- new variable

print(array) -- test this code to see that i added C to the array

2 Likes

Yes.

local array = {["A"] = {},["B"]={}} -- empty table, only for showcase

array["C"] = {} -- new variable

if array["C"] then
	print(true)
else
	print(false)
end

Output: true