DataStore2 Saving tables, from modules using serialization help?

--// Services
local ServerStorage = game:GetService("ServerStorage")


--// Modules
local Datastore2 = require(ServerStorage:WaitForChild("Modules"):WaitForChild("DataStore2"))
local Weapons = require(script.Weapons)
local Armour = require(script.Armour)
local Potions = require(script.Potions)

--// Tables
local WeaponTable = (Weapons.WeaponsTable)
local ArmourTable = (Armour.ArmourTable)
local PotionTable = (Potions.PotionTable)

--// DataStores
Datastore2.Combine("Inventory", "Gold", "Armour", "Potions", "Weapons","Health","Damage","MagicDamage","Level", "Experience","MaxExperience")

--// Player Events
game.Players.PlayerAdded:Connect(function(player)
	
	local goldStore = Datastore2("Gold", player); local goldStoreData = goldStore:Get()
	local armourStore = Datastore2("Armour", player)
	local potionsStore = Datastore2("Potions", player)
	local weaponsStore= Datastore2("Weapons", player)
	local healthStore = Datastore2("Health", player); local healthStoreData = healthStore:Get()
	local damageStore = Datastore2("Damage", player); local damageStoreData = damageStore:Get()
	local magicStore = Datastore2("MagicDamage", player); local magicStoreData = magicStore:Get()
	local levelStore = Datastore2("Level", player); local levelStoreData = levelStore:Get()
	local xpStore = Datastore2("Experience", player); local XpStoreData = xpStore:Get()
	local maxxpStore = Datastore2("MaxExperience",player); local MaxXpStoreData = maxxpStore:Get()
	
	armourStore:BeforeInitialGet(function(serialized)
	
	local deserialized = {}
	
		for _, id in pairs(serialized) do
			local itemName = ArmourTable[id]
			deserialized[itemName] = true
		end
		
		return deserialized
	end)
	
	armourStore:BeforeSave(function(deserialized)
		
		local serialized = {}
		
		for itemName in pairs(deserialized) do
			for itemId, name in pairs(ArmourTable) do
				if name == itemName then
					table.insert(serialized, itemId)
				end
			end
		end
		
		return serialized
		
	end)

end)

So this is what I currently have for my datastore script, I’ve only done one Serialized/Deserialized for now just incase this is wrong. So the question I want to ask is would this work? I have my tables in module script so I can easily edit them. I am using Serialization, but I’m not the best with tables and not sure if this would work, I have read everything on dataStore2 but for some reason my brian dies when I try understanding tables.

Would this work?
How would I change values?
Is it by doing ArmourTable[1]["Owned"] = true
If my table looked like this;

      [1] = {
    		["Name"] = "Leather Armour",
    		["Owned"] = true,
    		["Damage"] = 5,
    		["Magic Damage"] = 2,
    		["Health"] = 0, 
    		["Rarity"] = "Common"
    	}

What if you could Own the same of multiple weapons (Duplicates)? Would I make a Serial for each upgrade [1.1], [1.2] -- Upgrade Serials with different stats etc

Sorry if I’m being dumb but I really need someone to explain this please. If there is a document that does exist explaining this with multiple stores can some some send me it please. Thanks for reading and any help would be appreciated. :heart:

1 Like