What do you want to achieve?
I want to remove a value(data) inside a dictionary
What is the issue?
I cannot remove the specific value(Data) that I want
**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_59296059 This image is from DataStore Editor
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).
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
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)
: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
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)