Hi, I am working on a CAD System although I am having difficulty handling an issue I’m needing help with. When an Officer wants to submit a BOLO (Be On The Lookout) for a specific person I am setting it in the DataStoreService, and then they have the ability to close the BOLO so other Officers are aware it has been handled. I am doing this by doing:
local Store = DataStoreService:GetDataStore("TestingBOLOS")
Store:SetAsync(player.Name, {
{
Name = "RANDOMCODE",
Closed = false,
Charges = {"RandomCharge", "OtherCharge"}
}
})
The “RANDOMCODE” would be a Randomly Generated Code as I don’t know what else to replace it as but how would I go about editing the “Closed” value to TRUE instead of FALSE and then using SetAsync() to update the DataStore.
local Store = DataStoreService:GetDataStore("TestingBOLOS")
local Value = Store:GetAsync(player.Name) -- It gets the old value
Value.Closed = true -- It changes "Closed" to true
Store:SetAsync(player.Name, Value) -- Finally, it saves the new value
But I plan on adding multiple of these. So a Player could have 3 of these, how would I find which one to set to Closed? Would I use a for loop and find which one has the Name to the one wanting to be Closed and then do v.Value.Closed = true?
Firstly, whenever accessing the DataStoreService, ALWAYS use a pcall() because it is a network request. This means it could fail internally.
Here is how you could do it:
local dataStore = --your data store here
local function updateValue(player)
local key = --whatever you use with a UserId to set and get data
local playerData
local success, err = pcall(function()
playerData = dataStore:GetAsync(key)
end)
if success and playerData then
if playerData["Closed"] then --assuming the name saved in is also "Closed", change to whatever you saved it in as
PlayerData["Closed"] = true
end
elseif not success and err then
updateValue(player)
end
end
Please note the error handling is not as efficient as it could be as this is just a brief example.
I’ll use the pcall() function in future but this doesn’t really help. If I did get the Value I’m wanting to set to .Closed = false then how would updating it even work? Obviously using UpdateAsync(), but using what? UpdateAsync(player.Name, ?)?
I’ve never really used UpdateAsync(), so I’m not sure about that. Is it that you want it to re-set the data? You could just use the SetAsync() function with the new data, but obviously UpdateAsync() would be more efficient. There may be some information about it in the documentation.
You could use the method I have shown above, and then add a function to use SetAsync() to set the new data, or you could search the documentation on how to use UpdateAsync().
I have read the documentation and it looks like SetAsync() runs faster in setting the data. Therefore, it might be more efficient to use GetAsync(), update the value, then use SetAsync().
Thank you. However, using SetAsync(), where would I put the Table? Because it goes, SetAsync(PLAYER, TABLE), what would I use for the TABLE after setting the Closed value to TRUE?
First, print(playerData) after updating Closed to check it is all correct.
It should be all correct because you are using the same table you just read from the DataStoreService.
Then, use SetAsync() with that table. I hope that answers the question!
local Data = DataStoreService:GetDataStore("TestingBOLOS")
local Store = Data:GetAsync(player.Name)
for i,v in pairs(Store) do
if v.Name == "RANDOMCODE" then
v.Closed = true
end
end
Data:SetAsync(player.Name, Store)
Which gets the data and sets the Closed value to true.
Then, you can use that same table recieved from that function, by which now has the updated value, and you can use SetAsync() within a pcall() to overwrite the old table with the updated information. I hope this helps!
local key = player.Name
local playerData
local success, err = pcall(function()
playerData = dataStore:GetAsync(key)
end)
if success and playerData then
if playerData["1234"] then
playerData["1234"].Closed = true
dataStore:SetAsync(player.Name, playerData)
print("Success!")
end
end
I’m glad I could help, but also remember to wrap SetAsync() in a pcall() because that can also throw errors.
You could add:
local function save(key, data)
local success, err = pcall(function()
dataStore:SetAsync(key, data)
end)
if success then
print("Saved!")
elseif not success and err then
warn("Not saved! Will retry shortly.")
save(key, data)
end
end
--and then, in the other function...
local key = player.Name --I'm pretty sure this was your key
save(key, playerData)
--where 'playerData' is pre-defined
Please note the error handling is still not optimised - the code I have written here will constantly retry on ANY error, not just internal. Remember that Luau is case-sensitive, so even one spelling mistake could cause this loop. Any DataStoreService request is a network request, meaning they all require a pcall().