Help storing inventory data

im trying to make an inventory system for my game where it saves when you leave, i can get the base working, but i would like to store data for each item, (example: amount of item or level, arrow (x6), wooden sword lvl 12)
how can i do this?

local inventory = {
	"Wooden Sword",
	"Wooden Bow",
	"Arrow (x6)" -- i would like to store x6 in the arrows name somewhere else but i dont know how
}
2 Likes

can someone help please? its been 2 hours with nothing

1 Like

Well why dont you just have the item as a key and then the amount they have as the value?

 local inventory = {
   ["Wooden Sword"] = 1,
   ["Wooden Bow"] = 1,
   ["Arrow"] = 6
}

@KiwiVR45

1 Like

Well first of all you need to loop through the Instances / Objects in the players backpack with a for loop then you also look through the character for any possible equipped tools to store that as well, then to group them all together you can just check if any of the objects match up and you can make all of those objects in a table. Make duplicates increase their amount by 1.

eg.

--// Services \\

local Players = game:GetService("Players")

--// Script \\

Players.PlayerRemoving:Connect(function(Player)
	
	local Character = Player.Character or Player.CharacterAdded:Wait()
	
	for Index, Instances in Player.Backpack:GetChildren() do
		
		if Instances:IsA("Tool") then
			
			
			
		end
	end
	
	for Index, Instances in Character:GetChildren() do
		
		if Instances:IsA("Tool") then
			
			
			
		end
	end
end)

This is just the base of what you should be doing. (Make sure to include game:BindToClose())

how would i store other values? like levels?

and also how do i get the full inventory of only names?

yeah no one understands for some reason im gonna close it

With the help of IntValues inside of the player you can make a variable for it upon the player leaving.

ok let me explain again, im NOT trying to figure out how to save it, im trying to figrue out how to store it as a list,
lets say im trying to do this, it works fine

local inventory = {
	"Wooden Sword",
	"Wooden Bow",
	"Arrow"
}
local frame = script.Parent
for i,v in inventory do
	local clone = script.Item:Clone()
	clone.Title.Text = v
	clone.Parent = frame
end

but then what if i wanted to display a level?

local inventory = {
	"Wooden Sword",
	"Wooden Bow",
	"Arrow"
}
local frame = script.Parent
for i,v in inventory do
	local clone = script.Item:Clone()
	clone.Title.Text = v
    clone.Level.Text = v.Level -- how do i do this?
	clone.Parent = frame
end

I’m guessing this is what you mean?

local inventory = {
	["Wooden Sword"] = {
		["Level"] = 9, -- set this to whatever
		
		-- Everything below here is optional --
		["Damage"] = 5,
		["Rarity"] = "Common",
	},
	
	["Wooden Bow"] = {
		["Level"] = 12, -- set this to whatever

		-- Everything below here is optional --
		["Damage"] = 10,
		["Rarity"] = "Common"
	},
	
	["Arrow"] = {
		["Level"] = 1, -- set this to whatever

		-- Everything below here is optional --
		["Damage"] = 2,
		["Rarity"] = "Common"
	}
}


for item, data in pairs(inventory) do
	local itemLevel = data.Level
end

Here’s how it works:

So the type of table I used is called a “Dictionary”

when looping through a dictionary, the variable item is the name (such as “Arrow”, “Sword”, “Armor Piece”) and the variable data is the data stored in there (since the data stored in the items is a table, you access it by doing Data.Level or Data.Damage)

Here’s the document if you want to learn more: https://create.roblox.com/docs/tutorials/fundamentals/coding-5/intro-to-dictionaries

is there any way to get the name without manually adding it in the table?

Sorry, what do you mean? Are you saying like if the player gets an item and you want to add it to the table?

like this is what im looking for but i want to display the name of the item too,

local inventory = {
	["Wooden Sword"] = {
		["Level"] = 9, -- set this to whatever
		
		-- Everything below here is optional --
		["Damage"] = 5,
		["Rarity"] = "Common",
	},
	
	["Wooden Bow"] = {
		["Level"] = 12, -- set this to whatever

		-- Everything below here is optional --
		["Damage"] = 10,
		["Rarity"] = "Common"
	},
	
	["Arrow"] = {
		["Level"] = 1, -- set this to whatever

		-- Everything below here is optional --
		["Damage"] = 2,
		["Rarity"] = "Common"
	}
}


for item, data in pairs(inventory) do
	local itemLevel = data.Level
local itemName = item -- Yes, this is how you do it
end

im trying that but it keeps giving me " Unable to assign property Text. string expected, got table"

nevermind i re-read it and found out, thanks!

1 Like

Also if you need any more help DM me I give the fastest solutions to anything that I can do