I am trying to figure out how to make a decent inventory system using Datastore2.
and i`m going back and forward trying to figure out what the best way is.
inventory background info and wishlist
- 27 player inventory slots
- bankslots start at 54 (2x27) → maybe upgradable later
- max stack count difers between bank and inventory
- ability to insert items → example (inventory:insert({“sword”,1,“shield”,1}))(and error if it fails)
- ability to destroy items-> example (inventory:Destroy({“sword”,1,“shield”,1}))(and error if it fails)
- ability to split stacks
- ability to transfer stacks between inventorys(bank,backpack) but alsow between slots
this is what I am trying currently
warning long scripy
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local ContentProvider = game:GetService("ContentProvider")
DataStore2 = require(script.DataStore2)
Inven = require(script.Inventory)
-------------------------------------
local datastores ={
"WorldId",
"X",
"Y",
"Z",
"respawn",
"died",
"BrawlingXp",
"ArcheryXp",
"CastingXp",
"RobustnessXp",
"ArmorXp",
"StrengthXp",
"SmithingXp",
"FletchingXp",
"EnchantingXp",
"MiningXp",
"WoodcuttingXp",
"FiremakingXp",
"FishingXp",
"FarmingXp",
"HerbloreXp",
"CookingXp",
"CraftingXp",
"WorshipingXp",
"AgilityXp",
"ThievingXp",
"SlayingXp",
"HuntingXp",
"SummoningXp",
"ConstructingXp",
"QuestsProgress",
"EqHead",
"EqBack",
"EqNeck",
"EqAmmunition",
"EqMain_Tool",
"EqTorso",
"EqOff_Tool",
"EqGloves",
"EqLegs",
"EqRing",
"EqFeet",
"Bac1",--backpack slot 1 item name
"Bac1Q", -- quantity in backpackslot 1
"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",--itemname in bankslot 1
"Ban1Q",--quantity of items in bankslot 1
"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",
"Ban54",
"Ban54Q",
}
-------------------------------------------
DataStore2.Combine("DATA",
"WorldId",
"X",
"Y",
"Z",
"respawn",
"died",
"BrawlingXp",
"ArcheryXp",
"CastingXp",
"RobustnessXp",
"ArmorXp",
"StrengthXp",
"SmithingXp",
"FletchingXp",
"EnchantingXp",
"MiningXp",
"WoodcuttingXp",
"FiremakingXp",
"FishingXp",
"FarmingXp",
"HerbloreXp",
"CookingXp",
"CraftingXp",
"WorshipingXp",
"AgilityXp",
"ThievingXp",
"SlayingXp",
"HuntingXp",
"SummoningXp",
"ConstructingXp",
"QuestsProgress",
"EqHead",
"EqBack",
"EqNeck",
"EqAmmunition",
"EqMain_Tool",
"EqTorso",
"EqOff_Tool",
"EqGloves",
"EqLegs",
"EqRing",
"EqFeet",
"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",
"Ban54",
"Ban54Q"
)
-end of combine-----------------------------
function callRemote(player, Subject, value) -- comunication towards client
ReplicatedStorage.Core.Remotes.RemoteEvent:FireClient(player,Subject,value)
end
Players.PlayerAdded:Connect(function(player) ---- main setup
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
WorldId = DataStore2("WorldId", player)
WorldId:BeforeSave(function()
WorldId:Set(game.PlaceId,player)
end)
end)
function OnServerEvent(player,Subject,...)
print(player,Subject,...)
if Subject == "invenswap" then
Inven.invenswap (player,...)
end
end
ReplicatedStorage.Core.Remotes.RemoteEvent.OnServerEvent:Connect(OnServerEvent)```
Itemdictionary just in case, skills have something similary
["Bronze_Coin"] = {
["Description"] = "the main human currency",
["BackpackStackSize"] = 1000,
["BankStackSize"] = 10000,
["Value"] = 1,
["ModelId"] = 0,
["ImageId"] = 0,
},
["Copper_Coin"] = {
["Description"] = "the main human currency",
["BackpackStackSize"] = 1000,
["BankStackSize"] = 10000,
["Value"] = 100,
["ModelId"] = 0,
["ImageId"] = 4230412396,
},
["Silver_Coin"] = {
["Description"] = "the main human currency",
["BackpackStackSize"] = 1000,
["BankStackSize"] = 10000,
["Value"] = 1000,
["ModelId"] = 0,
["ImageId"] = 0,
},
["Gold_Coin"] = {
["Description"] = "the main human currency",
["BackpackStackSize"] = 1000,
["BankStackSize"] = 10000,
["Value"] = 10000,
["ModelId"] = 0,
["ImageId"] = 0,
},
["Sword"] = {
["Description"] = "a training sword",
["BackpackStackSize"] = 1,
["BankStackSize"] = 1,
["Value"] = 0,
["ModelId"] = 0,
["ImageId"] = 711844382,
},
}
MetaTable = { __index = function(table, key)
return {["Description"] = "Item info is missing",
["BackpackStackSize"] = "inf",
["BankStackSize"] = "inf",
["Value"] = 0,
["ModelId"] = 0,
["ImageId"] = 175158448}
end}
setmetatable(ItemDictionary,MetaTable)
return ItemDictionary```
for the XP part this works like a charm, easy to understand and to implement
but as mentioned above i am hesitant about the inventory system
I have played with a alternatives
keep in mind that I didn’t save my alternatives and this might have bugs
Alternative 1, tables
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local ContentProvider = game:GetService("ContentProvider")
local HttpService = game:GetService("HttpService")
DataStore2 = require(script.DataStore2)
Inven = require(script.Inventory)
-------------------------------------
local datastores ={
"WorldId",
"X",
"Y",
"Z",
"respawn",
"died",
"BrawlingXp",
"ArcheryXp",
"CastingXp",
"RobustnessXp",
"ArmorXp",
"StrengthXp",
"SmithingXp",
"FletchingXp",
"EnchantingXp",
"MiningXp",
"WoodcuttingXp",
"FiremakingXp",
"FishingXp",
"FarmingXp",
"HerbloreXp",
"CookingXp",
"CraftingXp",
"WorshipingXp",
"AgilityXp",
"ThievingXp",
"SlayingXp",
"HuntingXp",
"SummoningXp",
"ConstructingXp",
"QuestsProgress",
}
-------------------------------------------
DataStore2.Combine("DATA",
"WorldId",
"X",
"Y",
"Z",
"respawn",
"died",
"BrawlingXp",
"ArcheryXp",
"CastingXp",
"RobustnessXp",
"ArmorXp",
"StrengthXp",
"SmithingXp",
"FletchingXp",
"EnchantingXp",
"MiningXp",
"WoodcuttingXp",
"FiremakingXp",
"FishingXp",
"FarmingXp",
"HerbloreXp",
"CookingXp",
"CraftingXp",
"WorshipingXp",
"AgilityXp",
"ThievingXp",
"SlayingXp",
"HuntingXp",
"SummoningXp",
"ConstructingXp",
"QuestsProgress",
"Backpack",
"Bank",
"Equiped"
)
------------------------------
function callRemote(player, Subject, value) -- comunication towards client
ReplicatedStorage.Core.Remotes.RemoteEvent:FireClient(player,Subject,value)
end
Players.PlayerAdded:Connect(function(player) ---- main setup
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
WorldId = DataStore2("WorldId", player)
WorldId:BeforeSave(function()
WorldId:Set(game.PlaceId,player)
end)
local DefaultBackpack = {
["1"]={["item"] = "",["Quantity"] = 0},
["2"]={["item"] = "",["Quantity"] = 0},
["3"]={["item"] = "",["Quantity"] = 0},
["4"]={["item"] = "",["Quantity"] = 0},
["5"]={["item"] = "",["Quantity"] = 0},
["6"]={["item"] = "",["Quantity"] = 0},
["7"]={["item"] = "",["Quantity"] = 0},
["8"]={["item"] = "",["Quantity"] = 0},
["9"]={["item"] = "",["Quantity"] = 0},
["10"]={["item"] = "",["Quantity"] = 0},
["11"]={["item"] = "",["Quantity"] = 0},
["12"]={["item"] = "",["Quantity"] = 0},
["13"]={["item"] = "",["Quantity"] = 0},
["14"]={["item"] = "",["Quantity"] = 0},
["15"]={["item"] = "",["Quantity"] = 0},
["16"]={["item"] = "",["Quantity"] = 0},
["17"]={["item"] = "",["Quantity"] = 0},
["18"]={["item"] = "",["Quantity"] = 0},
["19"]={["item"] = "",["Quantity"] = 0},
["20"]={["item"] = "",["Quantity"] = 0},
["21"]={["item"] = "",["Quantity"] = 0},
["22"]={["item"] = "",["Quantity"] = 0},
["23"]={["item"] = "",["Quantity"] = 0},
["24"]={["item"] = "",["Quantity"] = 0},
["25"]={["item"] = "",["Quantity"] = 0},
["26"]={["item"] = "",["Quantity"] = 0},
["27"]={["item"] = "",["Quantity"] = 0},
}
local Backpack = DataStore2("Backpack", player)
backpackinv2 = Backpack:Get(DefaultBackpack)
--print(backpackinv2)
--Backpack:Set(DefaultBackpack)
callRemote(player,backpackinv2)
Backpack:OnUpdate(function (value)callRemote(player,backpackinv2)end)
local DefaultBank = {
["1"]={["item"] = "",["Quantity"] = 0},
["2"]={["item"] = "",["Quantity"] = 0},
["3"]={["item"] = "",["Quantity"] = 0},
["4"]={["item"] = "",["Quantity"] = 0},
["5"]={["item"] = "",["Quantity"] = 0},
["6"]={["item"] = "",["Quantity"] = 0},
["7"]={["item"] = "",["Quantity"] = 0},
["8"]={["item"] = "",["Quantity"] = 0},
["9"]={["item"] = "",["Quantity"] = 0},
["10"]={["item"] = "",["Quantity"] = 0},
["11"]={["item"] = "",["Quantity"] = 0},
["12"]={["item"] = "",["Quantity"] = 0},
["13"]={["item"] = "",["Quantity"] = 0},
["14"]={["item"] = "",["Quantity"] = 0},
["15"]={["item"] = "",["Quantity"] = 0},
["16"]={["item"] = "",["Quantity"] = 0},
["17"]={["item"] = "",["Quantity"] = 0},
["18"]={["item"] = "",["Quantity"] = 0},
["19"]={["item"] = "",["Quantity"] = 0},
["20"]={["item"] = "",["Quantity"] = 0},
["21"]={["item"] = "",["Quantity"] = 0},
["22"]={["item"] = "",["Quantity"] = 0},
["23"]={["item"] = "",["Quantity"] = 0},
["24"]={["item"] = "",["Quantity"] = 0},
["25"]={["item"] = "",["Quantity"] = 0},
["26"]={["item"] = "",["Quantity"] = 0},
["27"]={["item"] = "",["Quantity"] = 0},
["28"]={["item"] = "",["Quantity"] = 0},
["29"]={["item"] = "",["Quantity"] = 0},
["30"]={["item"] = "",["Quantity"] = 0},
["31"]={["item"] = "",["Quantity"] = 0},
["32"]={["item"] = "",["Quantity"] = 0},
["33"]={["item"] = "",["Quantity"] = 0},
["34"]={["item"] = "",["Quantity"] = 0},
["35"]={["item"] = "",["Quantity"] = 0},
["36"]={["item"] = "",["Quantity"] = 0},
["37"]={["item"] = "",["Quantity"] = 0},
["38"]={["item"] = "",["Quantity"] = 0},
["39"]={["item"] = "",["Quantity"] = 0},
["40"]={["item"] = "",["Quantity"] = 0},
["41"]={["item"] = "",["Quantity"] = 0},
["42"]={["item"] = "",["Quantity"] = 0},
["43"]={["item"] = "",["Quantity"] = 0},
["44"]={["item"] = "",["Quantity"] = 0},
["45"]={["item"] = "",["Quantity"] = 0},
["46"]={["item"] = "",["Quantity"] = 0},
["47"]={["item"] = "",["Quantity"] = 0},
["48"]={["item"] = "",["Quantity"] = 0},
["49"]={["item"] = "",["Quantity"] = 0},
["50"]={["item"] = "",["Quantity"] = 0},
["51"]={["item"] = "",["Quantity"] = 0},
["52"]={["item"] = "",["Quantity"] = 0},
["53"]={["item"] = "",["Quantity"] = 0},
["54"]={["item"] = "",["Quantity"] = 0},
}
local Bank = DataStore2("Bank", player)
Bankinv2 = Bank:Get(DefaultBank)
--print(backpackinv2)
--Bank:Set(DefaultBank)
callRemote(player,Bankinv2)
Bank:OnUpdate(function (value)callRemote(player,Bankinv2)end)
local DefaultEquiped ={
["Head" ]= "",
["Back" ]= "",
["Neck" ]= "",
["Ammunition"]= "",
["Main_Tool" ]= "",
["Torso" ]= "",
["Off_Tool"]= "",
["Gloves" ]= "",
["Legs"]= "",
["Ring"]= "",
["Feet"]= "",
}
local Equiped = DataStore2("Equiped", player)
Equipedinv2 = Equiped:Get(DefaultEquiped)
--print(Equipedinv2)
--Equiped:Set(DefaultEquiped)
callRemote(player,Equipedinv2)
Bank:OnUpdate(function (value)callRemote(player,Equipedinv2)end)
end)
function OnServerEvent(player,Subject,...)
print(player,Subject,...)
if Subject == "invenswap" then
Inven.invenswap (player,...)
end
end
ReplicatedStorage.Core.Remotes.RemoteEvent.OnServerEvent:Connect(OnServerEvent)
my question is :
based on Datastore2. what would be the best way to manage a inventory system.
i know other people have simulair issues,
ended up reading most of the messages here, watching the alvin blox video and RDC 2019
going to the github yet my doubt prevails