Data store with tools not working

Hello!

I have used a script that supposedly saves the player’s tools in their inventory. But it has not.

Script:

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

local player_data = DataStoreService:GetDataStore("player_data")

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

Players.PlayerAdded:Connect(function(client)
	local key = "client_" .. client.UserId
	local inventory = player_data:GetAsync(key) -- Not worrying about pcalls, do that yourself

	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

	player_data:UpdateAsync(key, function(prev)
		return tools
	end)

	inventory_folder:Destroy()
end)

Explorer:
image_2025-02-16_120636257

Any help?

1 Like

Sorry for the bump up, but i haven’t got a response in 25 mins.

You should try debugging via prints. Maybe print out inventory when they join and the tools table before saving.

This print statement didn’t print

1 Like

It’s probably an issue with your player-added event. When it fires are there tools in the players folder within the Inventories folder?

1 Like

There is nothing in the folders. Because the tools get added to the player’s backpack when interacting with a proxpromt.

I want it to save the tools into folders when the player leaves
and the loads the tools when the player joins.

1 Like

I’m saying within the players folder before they leave are there tools in there?

1 Like

No there are no tools in the players folder before they leave.

1 Like

If there are no tools within the player’s folder under Inventories then nothing can be saved. Your save works around the tools within the player’s folder so if there’s nothing then nothing saves.

1 Like

I forgot to mention the tools are in the player’s backpack before they leave. How would you make it so all of the tools clone to the folder when they leave?

1 Like

You can switch your leave event to this then if they’re exclusively on the player.

Players.PlayerRemoving:Connect(function(client)
	local key = "client_" .. client.UserId
	local tools = { }
	local inventory_folder = inventories[client.Name]

	local Character = client.Character
	if Character then
		for X, item in Character:GetChildren() do
			if not item:IsA("Tool") then continue end

			table.insert(tools, item.Name)
			local tool = item:Clone() -- Maybe these few lines
			tool.Name = item.Name -- ^
			tool.Parent = inventory_folder -- ^
		end
	end
	
	for _, item in client.Backpack:GetChildren() do
		table.insert(tools, item.Name)
	end
	
	player_data:UpdateAsync(key, function(prev)
		print(tools)

		return tools
	end)

	inventory_folder:Destroy()
end)

I got this error:
13:23:58.930 ServerScriptService.ToolSaving:33: attempt to iterate over a Instance value - Server - ToolSaving:33

Re-copy the code I updated it a minute after

I have gotten this error:
13:27:22.519 Dishes is not a valid member of Folder “ServerStorage.Tools” - Server - ToolSaving:21

The tools save but don’t load.

Is a tool called “Dishes” in your tools folder?

Yes.

Character requirement

another character requirement

Can you send a screenshot of your tools folder(ServerStorage.Tools) and everything in it? That error is occurring only because a tool with the specific name “Dishes” isn’t under the tools folder (ServerStorage.Tools)

The tools don’t go in the tools folder, instead they print as a table.

No I’m saying can you check your ServerStorage.Tools folder to see if there’s a child called “Dishes”? If you’re adding tools that arent in the tools folder then it cant clone a non existing tool

Nope there is nothing in the tools folder.

image_2025-02-16_134014781