How do I delete values with data store?

Hey! I have a slight issue with my datasave code. I have it so I can save things in tables, however I cant delete the values that arent there in the players database. I have no idea how I would go about this. Could someone please help?
Here is the code:

local valid_codes = { -- Codes Player Can Use
	["BETARELEASE"] = "50*",
	["EarlyAccess2023"] = "100",
	["JUMPSTART8"] = "nil^",
}

Players.PlayerAdded:Connect(function(Player) -- Activates on player joining
	local DataFromStore = nil

	local codefolder = Instance.new("Folder") -- Creates Folder
	codefolder.Name = "codefolder"
	codefolder.Parent = Player

	for name, value in pairs(valid_codes) do -- Creates Data in Folder
		local new = Instance.new("StringValue")
		new.Name = name
		new.Value = value
		new.Parent = codefolder
	end

	local s, e = pcall(function() -- Gets Data
		DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
	end)

	if s then -- Prints and Debugs
		print("Getting Data For: " .. Player.Name)
		if DataFromStore then
			for name, value in pairs(DataFromStore) do
				Player.codefolder[name].Value = value -- Replaces Data
			end
			print("Codes loaded: " .. Player.Name)
		else
			print("Created New Data For: " .. Player.Name)
		end
	else 
		warn("Codes Error: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}

	for name, value in pairs(Player.codefolder:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end

	local success,err = pcall(function()
		TestDataStore:SetAsync("uid-"..Player.UserId, DataForStore)
	end)

	if success then
		print("Successfully Saved Data For: " .. Player.Name)
	else
		warn(err)
	end
end)

In game codes are deleted on use.

You can resave the data without the data value that you want to remove.

Could expand on this? I dont think i understand fully

So when you leave the game, you save your data as it is. This time around, modify your save function so you no longer have the value that you wanted to save and it will be removed from your data store.

1 Like

Ok thank you, so I check if value doesnt exist then destroy

Well, not exactly, but if that was your intention to do that, that could work too.

Everytime you join the game you’re creating the Codes then saving them when you leave. So even if you did remove it from the DataStore you’d just be creating it again when you join back.

ok, well the original intention was to save it and retrieve what you had on a previous datasave

ok so how do I retrieve with this. Because I have this same table function in another game and it works just fine, but it doesnt have values that are getting deleted

What is actually happening here? What does 50*, 100 and nil^ even do?

Honestly the best bet would be to just create the StringValue when it is Redeemed and save the ones that have been redeemed rather than removing them

50* is to show what type of code it is. The “*” means its an amaount of coins granted to the player, and the “^” is for an item.

1 Like

I think your right. I will change it to this

1 Like

I am not sure what you want so I hope this helps

PS: enter this in command bar!

game:GetService("DataStoreService"):GetDataStore("Your Data name"):RemoveAsync("userid")

I figured out how to fix it. And I have the whole script if you want, instead of removing code values when they are used to prevent multiple users. I instead added them to the data store, and my full script works flawlessly. You can definately use it if you need.

local remoterecieve = game.ReplicatedStorage.CodeProcess
local remotesend = game.ReplicatedStorage.CodeSend
local datastoreservice = game:GetService("DataStoreService")
local mystore = datastoreservice:GetDataStore("mydatastore")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("emp43")

local function findstringfromdictionary(string, dictionary)
	for id, value in pairs(dictionary) do
		if value == dictionary then
			return print("Found string:", value, "with the id of:", id)
		end
	end
end

local original_codes = {
	["BETARELEASE"] = "50*",
	["EarlyAccess2023"] = "100",
	["JUMPSTART8"] = "nil^",
}

local invalid_codes = { -- Codes Player Can't Use
	["debug828amogus432"] = "nil^"
}

Players.PlayerAdded:Connect(function(Player) -- Activates on player joining
	local DataFromStore = nil

	local codefolder = Instance.new("Folder") -- Creates Folder
	codefolder.Name = "codefolder"
	codefolder.Parent = Player

	local s, e = pcall(function() -- Gets Data
		DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
	end)

	if s then -- Prints and Debugs
		print("Getting Data For: " .. Player.Name)
		if DataFromStore then
			for name, value in pairs(DataFromStore) do
				local loadcode = Instance.new('StringValue')
				loadcode.Name = name
				loadcode.Value = value
				loadcode.Parent = Player.codefolder
			end
			print("Codes loaded: " .. Player.Name)
		else
			print("Created New Data For: " .. Player.Name)
			for name, value in pairs(invalid_codes) do -- Creates Data in Folder
				local new = Instance.new("StringValue")
				new.Name = name
				new.Value = value
				new.Parent = codefolder
			end
		end
	else 
		warn("Codes Error: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}

	for name, value in pairs(Player.codefolder:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end

	local success,err = pcall(function()
		TestDataStore:SetAsync("uid-"..Player.UserId, DataForStore)
	end)

	if success then
		print("Successfully Saved Data For: " .. Player.Name)
	else
		warn(err)
	end
end)


remoterecieve.OnServerEvent:Connect(function(player, code) -- player who sent the request is automatically passed, code is the code
	local codes = player.codefolder
	if codes:FindFirstChild(code) == nil then -- finds code
		local foundcode = original_codes[code] -- gets code under table
		if string.find(foundcode, "*") then -- A '*' at the end of the tables string identifies what you will give the player
			local numvar2 = foundcode
			numvar2 = numvar2:sub(1, #numvar2-1) -- removes the '*'
			local nomorecode = Instance.new('StringValue')
			nomorecode.Name = code
			nomorecode.Value = foundcode
			nomorecode.Parent = player.codefolder
			invalid_codes[code] = foundcode
			player.leaderstats.Coins.Value += tonumber(numvar2)  -- Grants coins to player
			remotesend:FireClient(player,'unredeem') -- Sends that code worked
		end

		if string.find(foundcode, "^") then -- repeats code from above but with different symbol
			local numvar2 = foundcode
			numvar2 = numvar2:sub(1, #numvar2-1)
			local nomorecode = Instance.new('StringValue')
			nomorecode.Name = code
			nomorecode.Value = foundcode
			nomorecode.Parent = player.codefolder
			invalid_codes[code] = foundcode
			remotesend:FireClient(player,'unredeem')
		end
	else
		if original_codes[code] then -- Checks if code is in table and not under player
			remotesend:FireClient(player,'prevredeem')
		else
			remotesend:FireClient(player,'Error') -- Just sends error message
		end
	end
end)