I want to enter my datastore from a script and then delete a specific thing in it
script.Parent.OnServerEvent:Connect(function(plr) local ds = game:GetService("DataStoreService"):GetDataStore("DFSave") local items = ds:GetAsync("Items-"..plr.UserId) --Its a table with a lot of stuff in it if items then for i,v in pairs(items)do local item = game.ReplicatedStorage.DFMoves:FindFirstChild(v) --Im checking the table if item then items[v]:Remove() --What can i say to delete this thing inside the datastore end end end end)
You need to remove the index from the table and re-save it on the datastore
script.Parent.OnServerEvent:Connect(function(plr) local ds = game:GetService("DataStoreService"):GetDataStore("DFSave") local items = ds:GetAsync("Items-"..plr.UserId) --Its a table with a lot of stuff in it if items then for i,v in pairs(items)do local item = game.ReplicatedStorage.DFMoves:FindFirstChild(v) --Im checking the table if item then table.remove(items, i) -- remove that index from the table end end end ds:SetAsync("Items-"..plr.UserId, items) -- Save the table without that index/item end)
RemoveAsync()
RemoveAsync() was a function created by ROBLOX to delete specific entries in a datastore used by the system. In its most basic functionality, the built-in requires one (1) argument to be inputted. This function is called from a datastore.
Syntax
The correct way to use this is:
datastore:RemoveAsync(key)
Solving your Problem
So, what you need to do is replace the datastore in my syntax example with your datastore object (object, not string) and replace key as also in my syntax example with the index of the entry you want to remove.
A Few Reminders
I realize that you are iterating through a table with pairs, which is good code used by advanced scripters. However, one thing that I did notice is that you have a slight misunderstanding of the difference between i and v. First off, in the line where you call the :Remove() function, you choose the instance to remove as "items[v]." Unfortunately, the correct syntax would be "items[i]," as whenever you are running pairs in a for loop, it returns index and value. What an index is, is something used to call it, like a key to a door. A value is what the key holds, or what is behind that door that you opened with the key. So, i would be the key, and v would be the value. Whenever you are trying to call something by referencing it from a table, be sure to call something by its index, not its value. In essence, "items[v]" would become "items[i]."
The next issue I realized is that you need to use pcall. As earlier mentioned by @NiroTurtle, pcall is a function that should always be used when making any http request, or anything that doesn't simply run on the server, it needs internet access. So to use pcall, we say:
local s,m = pcall(function() --code here end)
s will return if the code embedded in the function returned an error of some sort, and if so, m will return any output that it gave. Now in your case, datastores do require the internet, so when using RemoveAsync, remember to wrap it in a pcall and handle it with a conditional, such as:
if not s then --This code will execute if the thing DID error. If so, m can be used to get the error message. For example, you could do something like this: warn('An error occured with the Async! Details: ' .. m) --This is called error handling, and it is VERY helpful when scripting, it will prevent your scripts from stalling and terminating like they would in a normal error. else --This code will execute if the thing worked. If so, here is an example of how you would use it: print('yass man the code worked :D') end
Finally, the answer!
Enough with that, I know I put you to sleep. With all the new things you learned, it is time we discover how you can fix the problem!
script.Parent.OnServerEvent:Connect(function(plr) local ds = game:GetService("DataStoreService"):GetDataStore("DFSave") local items = ds:GetAsync("Items-"..plr.UserId) --Its a table with a lot of stuff in it if items then for i,v in pairs(items)do local item = game.ReplicatedStorage.DFMoves:FindFirstChild(v) --Im checking the table if item then local s,m = pcall(function() items:RemoveAsync(i) end) if not s then --Handle the error here. Remember to use the example from above. else --Your code worked, put any additional things that should happen after the data is removed here. end end end end end)
I hope this helped, please leave a comment, and don't hesitate to contact me if you approach any further questions or errors. Happy coding! :D