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()
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
warn('An error occured with the Async! Details: ' .. m)
else
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)
if items then
for i,v in pairs(items)do
local item = game.ReplicatedStorage.DFMoves:FindFirstChild(v)
if item then
local s,m = pcall(function()
items:RemoveAsync(i)
end)
if not s then
else
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
1 comment
When you use :GetAsync(), you should wrap it in a pcall and repeat it a couple times in case it fails. It has happened to me before, my friend lost his stats in my game.