I worked on an inventory system not too long ago, I can think of 2 solutions for your problem
EDIT: I made a test place that has a basic item give shop and date store setup(Uncopylocked)
Inventory Data Store Test Place
Use the name of the item as the name to save
Make it an IntValue and use the value as the number of the item
The way I did it a while ago since I had multiple things to save for the item was placing values inside of the item and saving/loading those
Example for #2
This will save all the values into a table and then load them 1 by 1 into your inventory folder
It automatically saves every 300 seconds with a 30 second delay between players
I am not very good at coding so I'm sure there are more efficient ways but this is my way of doing it
local DSS = game:GetService("DataStoreService")
local set = "example1"
local invds = DSS:GetDataStore("inventory"..set)
game.Players.PlayerAdded:connect(function(player)
local key = tostring(player.UserId..set)
local inventory = Instance.new("Folder", player)
inventory.Name = "inventory"
if invds:GetAsync(key) ~= nil then
local tablea1 = invds:GetAsync(key)
for k,v in pairs(tablea1) do
local itemname = Instance.new("StringValue",player.inventory)
itemname.Name = v[1]
local itemlevel = Instance.new("IntValue",itemname)
itemlevel.Value = v[2]
itemlevel.Name = "Level"
local itemamount = Instance.new("IntValue",itemname)
itemamount.Value = v[3]
itemamount.Name = "Amount"
end
else
local iddata = {}
invds:SetAsync(key, iddata)
end
end)
function SaveData(player)
local key = tostring(player.UserId..set)
invds:UpdateAsync(key, function(OldValue)
if type(OldValue) == "table" then
local tablea1 = {}
for k,v in pairs(player.inventory:GetChildren()) do
local tabledata = {v.Name, v["Level"].Value, v["Amount"].Value}
table.insert(tablea1, tabledata)
end
OldValue = tablea1
end
return OldValue
end)
end
game.Players.PlayerRemoving:Connect(SaveData)
while wait(300) do
for k,v in pairs(game.Players:GetChildren()) do
wait(30)
SaveData(v)
end
end
7 comments
What? Can you explain the error?
There is no error, but I need a kind of way to store the value of an item someone has. For example: Player1 has 21 Items.
Let's say you want to leave in value a texture saved?
Not sure if I'm being clear enough but what I do mean is I want to save the quantity/amount of an item someone has
Huum, this script not saving itens correct? if no i can post a script that save.
no the script itself saves items properly but I need to know how I can make it so it will save the ammount of that item the players have. Like I said before. So again for example when someone picks up an item they already have it would show "item x2" meaning they have 2 of item
I'm looking for this too lol. i try to make this~