Advice on making a DataStore currency store and inventory system

Hi, I was thinking about making a DataStore currency store and inventory system, but what’s the best way to do it? don’t know if my ideas are the best ways to do.

Is it right to make a DataStore like this for every player?

Folder (Player name)
– --| Inventory
– – – --| Equipped item
– – – – – --| Sword
– – – --| Items
– – – – – --| Gun
– – – – – --| Axe
– – – --| Pets
– – – – – --| Value (Pet mesh ID)
– – – --| Equipped pet
– – – – – --| Value (Pet mesh ID)

1 Like

That looks like a pretty solid way to do it. Just a few comments:

  1. Make sure that you are using DS2 or some integrate some form of serverside messaging for the equipped items. Sometimes plain datastores will cache the value and return an old one. I personally use value instances, but those are deprecated and should not be used for new projects.
  2. Consider making this a table, and use DS2 to keep it abstract as a table, without making a physical instance (i.e. folders, value instances, etc.). I made this mistake on my project and it can make it difficult for backward compatibility.
  3. USE SERIALIZATION. This one is important, especially for large pet systems. Assign each item/pet a specific, unique number that can be used to identify it. This makes everything a lot easier if you ever have to change something later on.
Code Example
local itemSerials = {
	[1] = {
		class = "Weapon";
		name = "Sword";
		price = 5;
		minLevel = 0;
	};
	[2] = {
		class = "Weapon";
		name = "Gun";
		price = 10;
		minLevel = 1;
	};
	[3] = {
		class = "Weapon";
		name = "Axe";
		price = 15;
		minLevel = 2;
	};
	[4] = {
		class = "Weapon";
		name = "Spear";
		price = 50;
		minLevel = 1;
	};
	[5] = {
		class = "Pet";
		name = "Dog";
		price = 200;
		minLevel = 5;
		meshId = 1123581321;
	};
	[6] = {
		class = "Pet";
		name = "Cat";
		price = 200;
		minLevel = 5;
		meshId = 97867564534;
	};
}

local userData = {
	Inventory = {
		equippedItem = 1;
		equippedPet = 6;
		ownedItems = {
			[1] = true
			[2] = true
			[3] = true
		};
		ownedPets = {
			[5] = true
			[6] = true
		};
	}
}

EDIT: I’ve updated the code sample to be more compatible with the reply below. Note that it only allows the user to have one of each item. To have multiple, you should substitute the true values for the quantity of items the user has.

4 Likes

What method do I need to use to give someone an item in their inventory table? (userData)

1 Like

You could do something like this if you only allow one of each item:

local function addItem(player, itemSerial)
	local userData = datastore:Get(player) --Sub in your datastore method here
	if userData[Inventory][ownedItems][itemSerial] ~= true then
		userData[Inventory][ownedItems][itemSerial] = true
	end
end
2 Likes

Can I still use value instances? What are the downsides?

Hmm this is unrelated to what OP asked, but this seems like a nicer way to do things not sure though. This is how I do my items is this fine too? I don’t really think it matters, but yeah.

	["Sword"] = {
		["purchasable"] = true,
		["cost"] = 10,
		["name"] = "Sword",
		["rarityText"] = "Common",
		["rarityTextColor"] = Common,
		["equipmentSlot"] = "Mainhand",
		["tier"] = 1,
		["stats"] = {
			["Strength"] = 3,
			["Agility"] = 2,
		}
	},
2 Likes