DataStore2 Backpack and bank

Include a standalone, bare-bones rbxl file with only the code you want reviewed.

  • Code Review is for reviewing specific parts of your code, and not your whole game.
  • Code Review is intended for improving already-working code. If you need help debugging your code, please use Scripting Support.

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    i want to make a inventory people have a number of inventory (27) and bank slots (2x27)
    every slot can hold an amount of items like swords or shields, (1 slot can be 5 shields)
  • What potential improvements have you considered?
    i first wanted to do a table local backpack = {[“sword”]=5, [“shield”] = 2} but with datastore 2 when i try to set it as a table somehow it reference itself.
    alsow your inventory can get disorginized
  • How (specifically) do you want to improve the code?
    everytime i need to work on this code my finger hurst from all the scrolling.
    there has to be a better way to do this
Summary
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

DataStore2 = require(script.DataStore2)
-------------------------------------
local datastores ={
	"Bac1", -- bac=backpack
	"Bac1Q",--bac1Q = backpack 1 quantity (the amount of items in that slot
	"Bac2",
	"Bac2Q",
	"Bac3",
	"Bac3Q",
	"Bac4",
	"Bac4Q",
	"Bac5",
	"Bac5Q",
	"Bac6",
	"Bac6Q",
	"Bac7",
	"Bac7Q",
	"Bac8",
	"Bac8Q",
	"Bac9",
	"Bac9Q",
	"Bac10",
	"Bac10Q",
	"Bac11",
	"Bac11Q",
	"Bac12",
	"Bac12Q",
	"Bac13",
	"Bac13Q",
	"Bac14",
	"Bac14Q",
	"Bac15",
	"Bac15Q",
	"Bac16",
	"Bac16Q",
	"Bac17",
	"Bac17Q",
	"Bac18",
	"Bac18Q",
	"Bac19",
	"Bac19Q",
	"Bac20",
	"Bac20Q",
	"Bac21",
	"Bac21Q",
	"Bac22",
	"Bac22Q",
	"Bac23",
	"Bac23Q",
	"Bac24",
	"Bac24Q",
	"Bac25",
	"Bac25Q",
	"Bac26",
	"Bac26Q",
	"Bac27",
	"Bac27Q",

	"Ban1", -- ban = bank
	"Ban1Q", -- Q = quantity
	"Ban2",
	"Ban2Q",
	"Ban3",
	"Ban3Q",
	"Ban4",
	"Ban4Q",
	"Ban5",
	"Ban5Q",
	"Ban6",
	"Ban6Q",
	"Ban7",
	"Ban7Q",
	"Ban8",
	"Ban8Q",
	"Ban9",
	"Ban9Q",
	"Ban10",
	"Ban10Q",
	"Ban11",
	"Ban11Q",
	"Ban12",
	"Ban12Q",
	"Ban13",
	"Ban13Q",
	"Ban14",
	"Ban14Q",
	"Ban15",
	"Ban15Q",
	"Ban16",
	"Ban16Q",
	"Ban17",
	"Ban17Q",
	"Ban18",
	"Ban18Q",
	"Ban19",
	"Ban19Q",
	"Ban20",
	"Ban20Q",
	"Ban21",
	"Ban21Q",
	"Ban22",
	"Ban22Q",
	"Ban23",
	"Ban23Q",
	"Ban24",
	"Ban24Q",
	"Ban25",
	"Ban25Q",
	"Ban26",
	"Ban26Q",
	"Ban27",
	"Ban27Q",
	"Ban28",
	"Ban28Q",
	"Ban29",
	"Ban29Q",
	"Ban30",
	"Ban30Q",
	"Ban31",
	"Ban31Q",
	"Ban32",
	"Ban32Q",
	"Ban33",
	"Ban33Q",
	"Ban34",
	"Ban34Q",
	"Ban35",
	"Ban35Q",
	"Ban36",
	"Ban36Q",
	"Ban37",
	"Ban37Q",
	"Ban38",
	"Ban38Q",
	"Ban39",
	"Ban39Q",
	"Ban40",
	"Ban40Q",
	"Ban41",
	"Ban41Q",
	"Ban42",
	"Ban42Q",
	"Ban43",
	"Ban43Q",
	"Ban44",
	"Ban44Q",
	"Ban45",
	"Ban45Q",
	"Ban46",
	"Ban46Q",
	"Ban47",
	"Ban47Q",
	"Ban48",
	"Ban48Q",
	"Ban49",
	"Ban49Q",
	"Ban50",
	"Ban50Q",
	"Ban51",
	"Ban51Q",
	"Ban52",
	"Ban52Q",
	"Ban53",
	"Ban53Q",
	"Ban53",
	"Ban54Q",
}
-------------------------------------------
DataStore2.Combine("DATA",
	"Bac1",
	"Bac1Q",
	"Bac2",
	"Bac2Q",
	"Bac3",
	"Bac3Q",
	"Bac4",
	"Bac4Q",
	"Bac5",
	"Bac5Q",
	"Bac6",
	"Bac6Q",
	"Bac7",
	"Bac7Q",
	"Bac8",
	"Bac8Q",
	"Bac9",
	"Bac9Q",
	"Bac10",
	"Bac10Q",
	"Bac11",
	"Bac11Q",
	"Bac12",
	"Bac12Q",
	"Bac13",
	"Bac13Q",
	"Bac14",
	"Bac14Q",
	"Bac15",
	"Bac15Q",
	"Bac16",
	"Bac16Q",
	"Bac17",
	"Bac17Q",
	"Bac18",
	"Bac18Q",
	"Bac19",
	"Bac19Q",
	"Bac20",
	"Bac20Q",
	"Bac21",
	"Bac21Q",
	"Bac22",
	"Bac22Q",
	"Bac23",
	"Bac23Q",
	"Bac24",
	"Bac24Q",
	"Bac25",
	"Bac25Q",
	"Bac26",
	"Bac26Q",
	"Bac27",
	"Bac27Q",

	"Ban1",
	"Ban1Q",
	"Ban2",
	"Ban2Q",
	"Ban3",
	"Ban3Q",
	"Ban4",
	"Ban4Q",
	"Ban5",
	"Ban5Q",
	"Ban6",
	"Ban6Q",
	"Ban7",
	"Ban7Q",
	"Ban8",
	"Ban8Q",
	"Ban9",
	"Ban9Q",
	"Ban10",
	"Ban10Q",
	"Ban11",
	"Ban11Q",
	"Ban12",
	"Ban12Q",
	"Ban13",
	"Ban13Q",
	"Ban14",
	"Ban14Q",
	"Ban15",
	"Ban15Q",
	"Ban16",
	"Ban16Q",
	"Ban17",
	"Ban17Q",
	"Ban18",
	"Ban18Q",
	"Ban19",
	"Ban19Q",
	"Ban20",
	"Ban20Q",
	"Ban21",
	"Ban21Q",
	"Ban22",
	"Ban22Q",
	"Ban23",
	"Ban23Q",
	"Ban24",
	"Ban24Q",
	"Ban25",
	"Ban25Q",
	"Ban26",
	"Ban26Q",
	"Ban27",
	"Ban27Q",
	"Ban28",
	"Ban28Q",
	"Ban29",
	"Ban29Q",
	"Ban30",
	"Ban30Q",
	"Ban31",
	"Ban31Q",
	"Ban32",
	"Ban32Q",
	"Ban33",
	"Ban33Q",
	"Ban34",
	"Ban34Q",
	"Ban35",
	"Ban35Q",
	"Ban36",
	"Ban36Q",
	"Ban37",
	"Ban37Q",
	"Ban38",
	"Ban38Q",
	"Ban39",
	"Ban39Q",
	"Ban40",
	"Ban40Q",
	"Ban41",
	"Ban41Q",
	"Ban42",
	"Ban42Q",
	"Ban43",
	"Ban43Q",
	"Ban44",
	"Ban44Q",
	"Ban45",
	"Ban45Q",
	"Ban46",
	"Ban46Q",
	"Ban47",
	"Ban47Q",
	"Ban48",
	"Ban48Q",
	"Ban49",
	"Ban49Q",
	"Ban50",
	"Ban50Q",
	"Ban51",
	"Ban51Q",
	"Ban52",
	"Ban52Q",
	"Ban53",
	"Ban53Q",
	"Ban53",
	"Ban54Q"
)
------------------------------

function callRemote(player, Subject, ...)
	print(player, Subject, ...)
	ReplicatedStorage.RemoteEvent:FireClient(player,Subject,...)
end

Players.PlayerAdded:Connect(function(player)
	for i,v in pairs (datastores) do
		getfenv()[v] = DataStore2(v, player)
		local value = getfenv()[v]:Get(0)
		callRemote(player,v,value)

		getfenv()[v]:OnUpdate(function (value)callRemote(player,v,value)end)
	end
end)
1 Like

You can use for a loop and combine each key every loop.

for _, v in ipairs(datastores) do
       DataStore2.Combine("DATA", v)
end

for some reason it can’t,
if I enter the keys 1 by 1 it works.
but if I do it like you described there are warnings that I’m using the datastore to much (regular datastore)

function DataStore2.Combine(mainKey, ...)
	for _, name in pairs({...}) do
		combinedDataStoreInfo[name] = mainKey
	end
end

You can either change the Combine function in the module or iterate through datastores and combine each key.

I don’t get one thing, why do you need a seperate Key for each slot? Couldn’t you just use a table and use it for defining slots?

this is a function of datastore2. (i would not do this in normal datastore)
it takes each key and puts it in 1 table

ServerScriptService = game:GetService("ServerScriptService")
DataStore2 = require(ServerScriptService.Core.DataStore2)

script.Parent.MouseClick:Connect(function(player)
	local coins = DataStore2("Coins", player)
	coins :Increment(500,0)
end)

without having to go through the table