Help with datastores

Explaining this will be confusing but i will try to do my best.

So, i have a crate system. When you open a crate a script creates a bool value inside a folder named Inventory inside the player. This is the script:

local ds = game:GetService("DataStoreService"):GetDataStore("CrateDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local key = "CrateItems-"..player.userId
	local folder = Instance.new("Folder",player)
	folder.Name = "Inventory"
	local save = ds:GetAsync(key)
	if save then
		for i = 1,#save do
			local temp = Instance.new("BoolValue",folder)
			temp.Name = save[i]
		end
	end
end)

game.ReplicatedStorage.GiveCrateItem.OnServerEvent:Connect(function(player,WinningItem)
	local key = "CrateItems-"..player.userId

	local bool = Instance.new("BoolValue",player.Inventory)
	bool.Name = WinningItem

	local activated = {}
	for i,v in pairs(player.Inventory:GetChildren()) do
		if v:isA("BoolValue") then
			table.insert(activated, v.Name)
		end
	end
	ds:SetAsync(key,activated)
end)

Now i’m trying to make a crafting system. If the player has the items needed i want to remove them and give them the new crafted item (If the player has the values needed in the inventory folder i want to remove them and create a new one with the crafted items name) But destroying the values works until the player rejoins. The datastore will add them back when the player rejoins.

My question is: How can i remove values from the datastore so they don’t appear again?

I think you don’t have to remove the values ​​from the datastore, but you have to save the right values.


local ds = game:GetService("DataStoreService"):GetDataStore("CrateDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local key = "CrateItems-"..player.userId
	
	local folder = Instance.new("Folder",player)
	
	folder.Name = "Inventory"
	
	local save = ds:GetAsync(key)
	
	if save then
		
		for i = 1,#save do
			
			local temp = Instance.new("BoolValue",folder)
			
			temp.Name = save[i]
			
		end
		
	end
	
end)

game.ReplicatedStorage.GiveCrateItem.OnServerEvent:Connect(function(player,WinningItem)
	
	local key = "CrateItems-"..player.userId

	local bool = Instance.new("BoolValue",player.Inventory)
	
	local item_to_remove = {} -- this table contains some value to remove
	
	bool.Name = WinningItem
	
	if WinningItem == "some object" then -- on the basis of which object you choose me you lose others
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
	elseif WinningItem == "some object" then -- on the basis of which object you choose me you lose others
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
	elseif WinningItem == "some object" then -- on the basis of which object you choose me you lose others
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
	end

	local activated = {}
	
	for i,v in pairs(player.Inventory:GetChildren()) do
		
		if v:isA("BoolValue") then
			
			table.insert(activated, v.Name)
			
		end
		
	end
	
	for i , v in pairs (activated) do -- check if you have any items to remove
	
		for index , value_you_remove in pairs (item_to_remove) do -- check if you have any items to remove

			if v == value_you_remove then -- check if you have any items to remove

				table.remove(activated,i)

			end

		end
	
	end
	
	ds:SetAsync(key,activated)
	
end)

I hope this helps, in case it is not so say it but without being bad.

I’m willing to think about it again if something doesn’t work

I had forgotten one thing:


local ds = game:GetService("DataStoreService"):GetDataStore("CrateDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local key = "CrateItems-"..player.userId
	
	local folder = Instance.new("Folder",player)
	
	folder.Name = "Inventory"
	
	local save = ds:GetAsync(key)
	
	if save then
		
		for i = 1,#save do
			
			local temp = Instance.new("BoolValue",folder)
			
			temp.Name = save[i]
			
		end
		
	end
	
end)

game.ReplicatedStorage.GiveCrateItem.OnServerEvent:Connect(function(player,WinningItem)
	
	local key = "CrateItems-"..player.userId

	local bool = Instance.new("BoolValue",player.Inventory)
	
	local item_to_remove = {} -- this table contains some value to remove
	
	bool.Name = WinningItem
	
	if WinningItem == "some object" then -- on the basis of which object you choose me you lose others
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
	elseif WinningItem == "some object" then -- on the basis of which object you choose me you lose others
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
	elseif WinningItem == "some object" then -- on the basis of which object you choose me you lose others
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
		table.insert(item_to_remove,1,"some object to remove") -- enter an object to remove
		
	end

	local activated = {}
	
	local control = function(i,v)
		
		for index , value_you_remove in pairs (item_to_remove) do -- check if you have any items to remove

			if v == value_you_remove then -- check if you have any items to remove

				table.remove(activated,i)
				
				return false

			end

		end
		
		return true
		
	end
	
	for i,v in pairs(player.Inventory:GetChildren()) do
		
		if v:isA("BoolValue") then
			
			table.insert(activated, v.Name)
			
		end
		
	end
	
	for i , v in pairs (activated) do -- check if you have any items to remove
	
		while control(i,v) == false do
			
			wait(0.2)
			
		end
	
	end
	
	ds:SetAsync(key,activated)
	
end)

this will prevent a bug from occurring,
don’t use the above version!