How can I saving items in a table and load it?

Hello, I’m OriginalDevelops and I’m trying to make a Player Inventory System. I’ve tried many tips but it didn’t work and then I’m going to ask you guys. I don’t know how to saving items in a table ( Folder ) and load it when player join a game! So how can I saving items in a table and load it with DataStoreService? Hope you guys can help me with this :slight_smile:

-- My Script

game.Players.PlayerAdded:Connect(function(plr)
	
	local plrInv = Instance.new("Folder")
	plrInv.Name = "PlayerInv"
	plrInv.Parent = plr
	
end)

You could use a for loop when the Players Leave.

function onPlayerLeave(plr)
   local data
   local Inventory = {} --The table where gonna save the items in.
   local Folder = game.ServerStorage.Folder --Make sure all items in the game is stored in that folder.
  for _,item in pairs(plr.Inventory:GetChildren()) do
      table.insert(Inventory,item)
   local succ,bad = pcall(function()
       data = Inventory
``` , that should give you an idea. Then when the player joins then you loop through the data then put it on the players inventory.

Can you give me an example of loop when player join a game?

When the player joins then

local data
pcall(function()
    data = DataService:GetAsync(plr.UserId)
     --loop through the data
     for i,v in pairs(data)
        game.ServerStorage[v]:Clone().Parent = plr.inventory

You can use

for i,v in pairs() do
end)

to save it into a Table then save the table to the DS.
Also used to load values in aswell.

1 Like

You can save with a table as long as you don’t use physical objects or instances to save. You can use :GetChildren() on the folder and iterate it with pairs like the previous replies said.

Reiterate the GetAsync data with pairs to readd items.

Example related post here.

1 Like