I'm trying to remove all the StringValues that are in a folder from both folders & save it to the DataStore

Hi, I have all the ‘pet’ StringValues in the ‘PetInventory’ folder. I want to remove all the ‘pet’ StringValues that are in the ‘PetDelete’ folder from both folders & save it to the DataStore.
It removes all the ‘pet’ StringValues in the ‘PetInventory’ folder & the ‘PetDelete’ folder & saves it to the DataStore which isn’t what I want.

For example, if I have 2 pet StringValues called ‘Jeff’ & ‘Cedrick’ in the ‘PetInventory’ folder & I have 1 pet StringValue called ‘Jeff’ in the ‘PetDelete’ folder, then I want ‘Jeff’ to be removed from the DataStore & ‘Cedrick’ to be saved because Jeff is in both folders.

I looked for solutions on the Developer Forum. Usually, I would work on figuring it out myself for longer but I am trying to release a game before I go on holiday tomorrow so I need to work this out as soon as possible.

local inventory = {} -- creates a table

local function saveData(plr) -- creates a function
	if plr:FindFirstChild("PetInventory") then -- checks that the folder 'PetInventory' is in the player
		
		--local inventory = {}
		
		for i, v in pairs(plr.PetInventory:GetChildren()) do -- Loops through the pets in the 'PetInventory' folder
			print("adding:") -- This does print
			print(v)
			
			table.insert(inventory,v.Name) -- Inserts them into the table
			
		end

		local success, errorMessage = pcall(function()
		PetDataStore:SetAsync(plr.UserId.."-pet",inventory) -- Inserts them into the DataStore
		
		end)
		
		if success then -- Checks for errors
			print("Data Saved")
		else
			print("ERROR: "..errorMessage)
		end
	end
	
	if plr:FindFirstChild("EquippedPet") then -- Ignore this, it isn't important
			local success, errorMessage = pcall(function()
				PetDataStore:SetAsync(plr.UserId.."-equippedPet",plr.EquippedPet.Value)
			end)
	end
end

local function removeData(plr) -- creates a function
	if plr:FindFirstChild("PetDelete") then -- checks that the folder 'PetDelete' is in the player

		--local inventory = {}

		for i, v in pairs(plr.PetDelete:GetChildren()) do -- Loops through the pet StringValues in the 'PetDelete' folder
			print("removing:") -- This doesn't print anything
			print(v)
			
			table.remove(inventory,v.Name)  -- Removes them from the table

		end

		local success, errorMessage = pcall(function()
			PetDataStore:RemoveAsync(plr.UserId.."-pet",inventory) -- I want it to only delete the pet StringValues in the 'PetDelete' folder but it deletes all the pet StringValues in the PetInventory folder aswell. I think the problem is here but I might be wrong.
		end)

		if success then -- Checks for errors
			print("Data Saved")
		else
			print("ERROR: "..errorMessage)
		end
	end
end

game.Players.PlayerRemoving:Connect(function(plr)
	saveData(plr)
	removeData(plr)
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		saveData(plr)
		removeData(plr)
	end
end)

The saveData() function works but I’m trying to add the removeData() function.
I’m very new to DataStores & scripting in general. I apoligize if this is confusing. If anyone has any questions, feel free to ask. Any help would be appreciated!

RemoveAsync removes all the data on the saved key so you’re going to have to use SetAsync to set the data right.

1 Like

Try this:

local function removeData(plr) -- creates a function
	if plr:FindFirstChild("PetDelete") then -- checks that the folder 'PetDelete' is in the player

		--local inventory = {}

		for i, v in pairs(plr.PetDelete:GetChildren()) do -- Loops through the pet StringValues in the 'PetDelete' folder
			print("removing:") -- This doesn't print anything
			print(v)

			table.remove(inventory,v.Name)  -- Removes them from the table
		end
		
		local PlayerData = PetDataStore:GetAsync(plr.UserId.."-pet")
		table.remove(PlayerData,table.find(PlayerData, inventory))

		local success, errorMessage = pcall(function()
			PetDataStore:SetAsync(plr.UserId.."-pet", PlayerData) 
		end)

		if success then -- Checks for errors
			print("Data Saved")
		else
			print("ERROR: "..errorMessage)
		end
	end
end
1 Like

Thanks but I got this error: Argument 2 missing or nil

For which line?
30letterssssss

1 Like

It’s on the line where it says:

print("ERROR: "…errorMessage)

Sorry, just edited my post. Try the new code.

1 Like

Thank you sooo much!! My game is almost ready to go on now!

1 Like