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!
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.
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??
}
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.
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
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.
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
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.
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
local array = {["A"] = {},["B"]={}} -- empty table, only for showcase
array["C"] = {} -- new variable
if array["C"] then
print(true)
else
print(false)
end