Is it possible to combine Datastores that do the same thing into 1 function?

Hello,

So I’m making a system with DataStore2, containing an inventory with 4 different categories. Each of the categories does that exact same thing, just within a different table

Here is the default table:

local defaultInventory = {
	Gears = {};
	Perks = {};
	Crates = {};
	Cosmetics = {};
}

Here are all 4 functions that I would like to shorten to 1 (if possible):

local function updateGears(updatedGears)
		
		for _, item in pairs(gears:GetChildren()) do item:Destroy() end
		
		for _, item in pairs(gearsStore:Get(updatedGears)) do
			local itemValue = Instance.new("BoolValue", gears); itemValue.Name = item
			
		end
	end
	
	local function updatePerks(updatedPerks)

		for _, item in pairs(perks:GetChildren()) do item:Destroy() end

		for _, item in pairs(perksStore:Get(updatedPerks)) do
			local itemValue = Instance.new("BoolValue", perks); itemValue.Name = item

		end
	end
	
	local function updateCrates(updatedCrates)

		for _, item in pairs(crates:GetChildren()) do item:Destroy() end

		for _, item in pairs(cratesStore:Get(updatedCrates)) do
			local itemValue = Instance.new("BoolValue", crates); itemValue.Name = item

		end
	end
	
	local function updateCosmetics(updatedCosmetics)

		for _, item in pairs(cosmetics:GetChildren()) do item:Destroy() end

		for _, item in pairs(cosmeticsStore:Get(updatedCosmetics)) do
			local itemValue = Instance.new("BoolValue", cosmetics); itemValue.Name = item

		end
	end

I was wondering how would I be able to do shorten this. Is this a professional way of doing this or?? I attempted to do this on my own, it worked fine the first time but when I left and came back, it just gave me errors:

local function updateInventory(updatedInventory, newTable)
		for category, categoryTable in pairs(inventoryStore:Get(updatedInventory)) do
			print(newTable)
			print(category)
			
			if newTable then
				for _, item in pairs(inventory:FindFirstChild(newTable):GetChildren()) do item:Destroy() end
				
				local categoryStore = inventoryStore:Get(updatedInventory)[newTable]
				for _, item in pairs(categoryStore) do
					local itemValue = Instance.new("BoolValue", inventory:FindFirstChild(category)); itemValue.Name = item

				end
			else
				print(newTable)
				for _, item in pairs(inventory:FindFirstChild(category):GetChildren()) do item:Destroy() end
				
				for key, updatedCategory in pairs(updatedInventory) do
					print(key)
					local categoryStore = inventoryStore:Get(updatedCategory)[category]

					if key == category then
						print(key)

						for _, item in pairs(categoryStore) do
							local itemValue = Instance.new("BoolValue", inventory:FindFirstChild(category)); itemValue.Name = item

						end
						break
					end				
				end

			end
		end
	end

I also made sure to use the DataStore2.Combine() for this:

DataStore2.Combine("Inventory", "Gears", "Perks", "Crates", "Cosmetics")

local inventoryStore = DataStore2("Inventory", player)

-- After the Update inventory function

updateInventory(defaultInventory, nil)
inventoryStore:OnUpdate(updateInventory)

My point is, is there any way to make the functions look more professional in one table rather than writing 4 of the same thing for different categories. I did attempt to do this but it gave me errors the second time after and I have no idea where to go from there. How would I fix that then?

    local function updateCategory(categoryFolder, categoryStore, updatedValues)

		for _, item in pairs(categoryFolder:GetChildren()) do item:Destroy() end

		for _, item in pairs(categoryStore:Get(updatedValues)) do
			local itemValue = Instance.new("BoolValue", categoryFolder); itemValue.Name = item
		end
	end

Or if you put all the category-related things into a dictionary:

local categories = {
    Cosmetics = {
        dataStore = categoryStore,
        folder = cosmetics,
    }, -- etc. ...
}

    local function updateCategory(category, updatedValues)
		for _, item in pairs(category.folder:GetChildren()) do item:Destroy() end

		for _, item in pairs(category.dataStore:Get(updatedValues)) do
			local itemValue = Instance.new("BoolValue", category.folder); itemValue.Name = item
		end
	end

updateCategory(categories.Cosmetics, newValues)
1 Like

so does that mean I have to rewrite this thing 4 times to get all 4 categories? I’m asking how would I be able to make 1 function that could work for all 4 categories, but they have different parents and stuff.

No, you’d just populate the “categories” table and only use the single updateCategory function. You could probably populate it automatically but can’t really know without seeing how you set e.g. inventory and inventoryStore

So when you say this??? How would I do it for the other 3? just write the same thing but instead e.x. updateCategory(categories.Perks, newValues) instead?

If I am correct heart it if I’m not just reply.

1 Like