Inventory Save Bug

Hello, it is me InquistorWasBanned. I followed AlvinBlox’s Video and the shop and the inventory saves. But there is a problem. Unlike my gold which saves with Datastore2, the inventory save sometimes lags and does not save completely. How can I fix that? This is my Inventory Save.

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

game.Players.PlayerAdded:Connect(function(plr)
	
	
	
	
	local data
	
	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(plr.UserId)
	end)
	if data ~= nil then
		for _, toolName in pairs(data) do
			
			local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
			
			if tool then
				local newTool = tool:Clone()
				newTool.Parent = plr.Backpack
				
				local newTool = tool:Clone()
				newTool.Parent = plr.StarterGear
				
			end
		end
	end	
end)
game.Players.PlayerRemoving:Connect(function(plr)
	
	local toolsTable = {}
	
	for _, tool in pairs(plr.Backpack:GetChildren()) do
		if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
			table.insert(toolsTable,tool.Name)
		end
	end
	
	
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(plr.UserId,toolsTable)
	end)
	
end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
			local toolsTable = {}
			
			for _, tool in pairs(plr.Backpack:GetChildren()) do
				if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
					table.insert(toolsTable,tool.Name)
				end
			end
			
			
			local success, errorMessage = pcall(function()
				DataStore:SetAsync(plr.UserId,toolsTable)
			end)	
		end
end)

Im no scripter myself but some AlvinBlox videos are old and won’t function with new roblox updates, so check to see if the video was recent.

AlvinBlox is not known to teach good practices.

This is false, his tutorials “work” but you shouldn’t use them, especially for saving data! I would read this

1 Like

Yes it works the Inventory Save actually works its jsut sometimes people lose some of their tools? So I was hoping to learn how to transfer that code or if possible switch it to DataStore2. I already have DataStore2 for my currency save.