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
}
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())
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
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
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)
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