Delete a table in a datastore

  1. What do you want to achieve?
    I’m working on a moderation panel for my game and I want to be able to delete moderation actions that have been previously taken. For example when someone accidentally warns someone they can delete it again.

  2. What is the issue?
    No matter what I try it never deletes it. My knowledge about datastore is really bad.

  3. What solutions have you tried so far?
    I have looked through multiple other forum posts but didnt find anything that fixed my problem.

This is the script I currently have. I just copied it over from my other script to create the warning in the datastore and tried setting it to nil but it always says “attempt to index nil with nil”

	local UserID = script.Parent.Parent.Parent.Avatar.UserId.Text
	local key = "Log_"..UserID
	local number = tonumber(goal)
	local DeleteData
	local success, err = pcall(function()
		DeleteData = WDS:GetAsync(key)
	end)
	
	DeleteData[number] = nil

	local success, err = pcall(function()
		WDS:SetAsync(key, DeleteData)
	end)


	if success then
		warn("Success")
	else
		warn("Fail")
	end
DeleteData -- this could be nil

if DeleteData and DeleteData[number] ~= nil then -- Checks if `DeleteData[number] != nil` then it sets it to nil
    DeleteData[number] = nil 
end

Attempt to index nil with nil tells me that tonumber(goal) returns nil as goal is not a number in string form. Double-check that. Also, using UpdateAsync might be easier here.

local success, result = pcall(WDS.UpdateAsync, WDS, key, function(oldDD)
    if oldDD and oldDD[number] then
        oldDD[number] = nil
    end

    return oldDD
end)

Idk if I did anything wrong but my script now looks like this

DeleteEvent.OnServerEvent:Connect(function(goal, Type)
	local UserID = script.Parent.Parent.Parent.Avatar.UserId.Text
	local key = "Log_"..UserID
	local DeleteData
	local number = 1 --Set it to 1 for testing
	
	local success, err = pcall(function()
		DeleteData = WDS:GetAsync(key)
	end)
	
	local success, result = pcall(WDS.UpdateAsync, WDS, key, function(oldDD)
		if oldDD and oldDD[number] then
			oldDD[number] = nil
		end

		return oldDD
	end)
end)

It gives me this error
image

Weird, I’ve never seen that error before in UpdateAsync. Make sure you have nothing causing it to wait at all. You also don’t need GetAsync for this.