How to save and load the players inventory?

As the title says, I am having trouble saving the players inventory, and loading it. I am new to DataStores, and the most I can do is save some leaderstats and clothing.

I have been coding for about 5 hours now, working on my wizard game, but I still can’t figure this out. I tried looping through the players inventory when they leave, but my brain just fries up from there and I forget what I’m doing.

If someone could give me an explanation on how to do this, I would be very grateful.

Click me (;

Check out this sweet climbing mechanic I made while you’re at it :cool:

1 Like

when leaving do :SetAsync() and when player join use :GetAsync() also the climbing is fire :fire::fire:

1 Like

I know that, but like, how. I feel like I have todler lvl IQ today
(and thank you :cool: I think the climbing is awesome)

do you have any script about this, ill explain on that


Just kidding, well not really.
I have a folder with all the items in the game that a player can collect, but I don’t know where to go from there.
image

1 Like

so i am in phone so i cant explain with a script but here is a simple explanation:

for example
local dataStore = getService(“DataStore”)getdatastore(“thisnameisntimportant”)

local playerData = dataStore:GetAsync(player.UserId)

------ When player is removing

dataStore:SetAsync(player.UserId, {“The data you want to write”})

1 Like

also if the player is joining for the first time playerData will be nil

Oh my god.
My brain just exploded and I remembered how to do everything.

local DataStoreService = game:GetService("DataStoreService")
local playerStuffStore = DataStoreService:GetDataStore("MyDataStore")

local players = game.Players

players.PlayerAdded:Connect(function(player)
	local inventoryData
	local success, err = pcall(function()
		inventoryData = playerStuffStore:GetAsync(player.UserId .. "-inventory")
	end)
	if success then
		if inventoryData ~= nil then
			for i = 1, #inventoryData do
				local tool = game.ServerStorage.Tools:FindFirstChild(inventoryData[i])
				if tool then
					tool:Clone().Parent = player.Backpack
				end
			end
		else
			
		end
	else
		print("There was a problem loading "..player.Name.."'s inventory. ->")
		warn(err)
	end
end)
players.PlayerRemoving:Connect(function(player)
	--loop through players inventory, and add tools to a table
	local inventory = {}
	for i, tool in pairs(player.Backpack:GetChildren()) do
		table.insert(inventory,tool.Name)
	end
	
	local success, err = pcall(function()
		playerStuffStore:SetAsync(player.UserId.."-inventory",inventory)
	end)
	if success then
		print("Inventory successfully saved!")
	else
		print("There was an error saving data ->")
		warn(err)
	end
end)

Thank you sm, because you kinda woke me up. You can have the solution :heart:

1 Like

Your Welcome, Have a nice day :hugs:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.