Datastore: Help with removing a value inside a key which is a dictionary

  1. What do you want to achieve?
    I want to remove a value(data) inside a dictionary

  2. What is the issue?
    I cannot remove the specific value(Data) that I wantStructure

  3. **What solutions have you tried so far?
    I have tried using table.remove but it doesn’t work, I also tried using sampleDataStore:RemoveAsync(playerKey).Data.Test but this just removes all values related to the key(Player_59296059)

SCRIPT:

local sampleDataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

game.Players.PlayerRemoving:Connect(function(player)
	local playerKey = "Player_59296059"
	
	
	local success, val = pcall(function()	
		return sampleDataStore:RemoveAsync(playerKey).Data
	end)
	
	if success then
		print(val)
	end

end)

OUTPUT:
true --This is the value related to the key Test

I only want to delete Test, this however deletes all data related to the key Player_59296059Result
This image is from DataStore Editor

3 Likes

Maybe try:

return sampleDataStore:RemoveAsync(playerKey).Data[Test]

Hello I tried this but the Test is Unknown global ‘Test’ and this prints nil

Since Data is also a dictionary, maybe try this:

return sampleDataStore:RemoveAsync(playerKey)[Data][Test]

Hi!

Unfortunately, I currently don’t have enough time to test it out, but I think you forgot the quotation marks.

return sampleDataStore:RemoveAsync(playerKey)["Test"]

EDIT @thehalfdog21 the above example supposes “Data” is the whole table, and test is one of its indexes.

1 Like

Hello I tried this but the DataUnknown global ‘Data’ is Test is Unknown global ‘Test’ and this prints nil

EDIT: It also removes all the data related to Player_59296059, Im assuming sampleDataStore:RemoveAsync(playerKey) is the only one being read because the all the data is still being removed

Since you’re already using sleitnick’s DataStore Editor plugin, you can modify the data directly by right clicking on “Test” and clicking “Delete” (if I remember correctly).

I also tried your code but it also delete all the data related to Player_59296059

Yes I also did that earlier but I am trying to figure it out using a script, I really don’t want to manually delete all Test in each key

1 Like

You need to update the data using either :UpdateAsync() or :SetAsync(), you can’t use :RemoveAsync() in this case:

local DataStoreService = game:GetService("DataStoreService")
local sampleDataStore = DataStoreService:GetDataStore("PlayerData")
local IsSuccessful, Result = pcall(function(Key)
    local Data = sampleDataStore:GetAsync(Key)
    Data.Test = nil
    sampleDataStore:SetAsync(Key, Data)
    return Data
end, "Player_59296059")

if IsSuccessful == true then
    print(Result)
end
2 Likes

I tried this but unfortunately it doesn’t work,

Script:


Player.PlayerAdded:Connect(function()
	local Key = "Player_59296059"
	local DataStoreService = game:GetService("DataStoreService")
	local sampleDataStore = DataStoreService:GetDataStore("PlayerData")
	local IsSuccessful, Result = pcall(function(Key)
		local Data = sampleDataStore:GetAsync(Key)
		Data.Test = nil
		sampleDataStore:SetAsync(Key, Data)
		return Data
	end, "Player_59296059")

	if IsSuccessful == true then
		print(Result) --Will only print if SetAsync() was successful
	end
	
end)

OUTPUT:
{
                    ["Data"] =   {
                       ["Cash"] = 0,
                       ["Items"] = {},
                       ["LogInTimes"] = 0,
                       ["Test"] = true
                    }
                 }

It’s kinda wierd that the SetAsync() successfully set Test to nil and yet Test still exist(I assume it was success because it printed the result)
Exist

:RemoveAsync() is used to remove data in a certain key. In this case, you need to update the data by using :SetAsync().

-- success: tells if it's success or not
-- result: will return nil (if success), otherwise it'll return error string
local success, response = pcall(function()
    local save_data = sampleDataStore:GetAsync(playerKey)
    save_data.Data.Test = nil
    sampleDataStore:SetAsync(Key, save_data)
    print(save_data.Data) -- prints the updated data
end)

if not success then
    error(response)
end
2 Likes

Thanks so much! I can now visualize it better.

This:

Player.PlayerAdded:Connect(function(player)
	local playerKey = "Player_59296059"
	local sampleDataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

	local success, response = pcall(function()
		local save_data = sampleDataStore:GetAsync(playerKey)
		save_data.Data.Test = nil
		sampleDataStore:SetAsync(playerKey, save_data)
		print(save_data.Data) -- prints the updated data
	end)

	if not success then
		error(response)
	end
end)

Is similar to this:

Player.PlayerAdded:Connect(function(player)
	local playerKey = "Player_59296059"
	local sampleDataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

	-- success: tells if it's success or not
	-- result: will return nil (if success), otherwise it'll return error string
	local success, response = pcall(function()
		local save_data = {
                    ["Data"] =   {
                       ["Cash"] = 0,
                       ["Items"] = {},
                       ["LogInTimes"] = 0,
                       ["Test"] = nil --Setting to nil to remove
                    }
                 }
		sampleDataStore:SetAsync(playerKey, save_data)
		print(save_data.Data) -- prints the updated data
	end)

	if not success then
		error(response)
	end
end)

My mistakes includes:

  • Using RemoveAsync() to delete a specific value inside a key(dictionary)
  • Doing this
local IsSuccessful, Result = pcall(function()
		
		local playerID = sampleDataStore:GetAsync(Key) 
		local Data = playerID.Data
		local Test = Data.Test
		local newData = false 
		
		sampleDataStore:SetAsync(Key,newData) --Which is basically overwriting all values related to the Key
		
	end)
1 Like