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?