Hey, so my datastore script for saving inventories isn't working at all. I checked the console, and it didn't print any errors..

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")

local player_data = DataStoreService:GetDataStore("player_data")
local cash_data = DataStoreService:GetDataStore("cash_store")

local tools = ServerStorage.Tools
local inventories = ServerStorage.Inventories

Players.PlayerAdded:Connect(function(client)
	local key = "client_" .. client.UserId
	local success, inventory = pcall(function()
		return player_data:GetAsync(key)
	end)
		
	local inventory_folder = Instance.new("Folder")
	inventory_folder.Name = client.Name
	inventory_folder.Parent = inventories

	for _, name in ipairs(inventory or { }) do
		local tool = tools[name]
		tool:Clone().Parent = client.Backpack -- For the player to use
		tool:Clone().Parent = inventory_folder -- For saving and loading
	end
end)

Players.PlayerRemoving:Connect(function(client)
	local key = "client_" .. client.UserId
	local tools = { }
	local inventory_folder = inventories[client.Name]
	
	for _, item in ipairs(inventory_folder:GetChildren()) do
		table.insert(tools, item.Name)
	end

	local success, err = pcall(function()
		player_data:UpdateAsync(key, function(prev)
			return tools
		end)
	end)
	
	if not success then
		warn(err)
	end

	inventory_folder:Destroy()
end)

I found the script off the devforum https://devforum.roblox.com/t/how-to-save-your-players-inventory-items/467961

EDIT: After some messing around with the code, I've figured that the pcalls are breaking the script because without them, it works fine. But obviously, you need pcalls in saving scripts..

2 comments

0

Best guess would be the "local success, inventory = pcall(function()" line, try with "local success, err, inventory = pcall(function()" instead?

0

It didn't work..