Data store with tools not working

Where are the tools you’re cloning located then?

Here is the player added script:

Players.PlayerAdded:Connect(function(client)
	print("playeradded")
	local key = "client_" .. client.UserId
	local inventory = player_data:GetAsync(key) 

	local inventory_folder = Instance.new("Folder")
	inventory_folder.Name = client.Name
	inventory_folder.Parent = inventories -- in my previous post, you can see the folder name, except the tools don't get cloned in there.

	for _, name in ipairs(inventory or { }) do
		print("for loop")
		local tool = tools[name]
		tool:Clone().Parent = client.Backpack 
		tool:Clone().Parent = inventory_folder -- it clones here
		print(tool[name])
	end
end)

No im not asking for where the tools get cloned into I’m asking where the original tools are :skull:

Like without playing the game where are the original tools that you clone and give to the players located

The tools aren’t there :frowning:

It says and error when joining the game

image_2025-02-16_134605951

The original tools are givin to the player’s backpack randomly when clicking a proxpromt

Then where are they? What I’m saying is if you want to give a player a sword you would clone the sword from server storage and place it into their backpack. In this case, it was originally located in the server storage. What I am asking is where the original tools are. Are they in replicated storage?

Where are the given tools in your game

Not quite sure what you mean but here is the script where the tools are given to the player.

player = game:GetService("Players").PlayerAdded:Wait() 


local RepliStorage = game:GetService("ReplicatedStorage")


local items = {
	{name = "Newspaper", weight = 20, cost = 3, Rarity = "Common"},
	{name = "Rat", weight = 20, cost = 2, Rarity = "Common"},
	{name = "Broken Furniture", weight = 2, cost = 70, Rarity = "Legendary"},
	{name = "Toy", weight = 13, cost = 8, Rarity = "Uncommon"},
	{name = "Bottle", weight = 20, cost = 3, Rarity = "Common"},
	{name = "Shoes", weight = 7, cost = 13, Rarity = "Rare"},
	{name = "Monitor", weight = 2, cost = 85, Rarity = "Legendary"},
	{name = "Clothes", weight = 13, cost = 10, Rarity = "Uncommon"},
	{name = "Scrap Metal", weight = 20, cost = 2, Rarity = "Common"},
	{name = "Cardboard", weight = 20, cost = 4, Rarity = "Common"},
	{name = "Book", weight = 20, cost = 4, Rarity = "Common"},
	{name = "Plastic Container", weight = 13, cost = 7, Rarity = "Uncommon"},
	{name = "Food Scraps", weight = 13, cost = 13, Rarity = "Uncommon"},
	{name = "Hammer", weight = 7, cost = 30, Rarity = "Rare"},
	{name = "Can", weight = 20, cost = 3, Rarity = "Common"},
	{name = "Magazines", weight = 13, cost = 12, Rarity = "Uncommon"},
	{name = "Dishes", weight = 7, cost = 27, Rarity = "Rare"},
	{name = "Batteries", weight = 7, cost = 25, Rarity = "Rare"},
	{name = "Desk", weight = 2, cost = 115, Rarity = "Legendary"},
	{name = "Chair", weight = 7, cost = 43, Rarity = "Rare"},
	{name = "Keyboard", weight = 2, cost = 58, Rarity = "Legendary"},
	{name = "Light", weight = 2, cost = 43, Rarity = "Legendary"},
	{name = "PC", weight = 1, cost = 468, Rarity = "Mythic"},
	{name = "Fridge", weight = 1, cost = 286, Rarity = "Mythic"},
	{name = "Microwave", weight = 1, cost = 286, Rarity = "Mythic"},
	{name = "Paint Brush", weight = 13, cost = 11, Rarity = "Uncommon"},
	{name = "Paint Can", weight = 13, cost = 7, Rarity = "Uncommon"},
}




local function getRandomItemInfo()
	local totalWeight = 0

	for _, item in items do
		totalWeight = totalWeight + item.weight
	end

	local randomWeight = math.random(1, totalWeight)
	local currentWeight = 0

	for _, item in items do
		currentWeight = currentWeight + item.weight
		if randomWeight <= currentWeight then
			return item.name, item.cost, item.Rarity
		end
	end
end

local Item = 0
local ItemCost = 0
local TotalItemCost = 0

local function onProximityPromptTriggered(plr)
	
	-- Minigame
	
	
	local selectedItem, selectedCost, selectedRarity = getRandomItemInfo()
	print("Player received: " .. selectedItem)
	print(selectedItem.. " is worth: " .. selectedCost)
	print(selectedItem.. " is " .. selectedRarity)

	if RepliStorage.ItemAssets:FindFirstChild(selectedItem) then
		local tool = RepliStorage.ItemAssets:FindFirstChild(selectedItem)
		if tool then
			tool:Clone()
			tool.Parent = plr.Backpack
			tool.Name = selectedItem
			Item = selectedItem
			ItemCost = selectedCost
		end
		
	else
		print(selectedItem.. " is not found in ReplicatedStorage")
		local tool = Instance.new("Tool")
		tool.Parent = plr.Backpack
		tool.Name = selectedItem
		local handle = Instance.new("Part")
		handle.Parent = tool
		handle.Name = "Handle"
		handle.Size = Vector3.new(1,1,1)
		Item = selectedItem
		ItemCost = selectedCost
	end

end

local dumpster = script.Parent

RepliStorage.Remotes.Dumpster1.OnServerEvent:Connect(function(plr)
	onProximityPromptTriggered(plr)
end)

This join event should now work then:

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 = game:GetService("ReplicatedStorage")["ItemAssets"][name]
		if not tool then continue end
		
		tool:Clone().Parent = client.Backpack -- For the player to use
		tool:Clone().Parent = inventory_folder -- For saving and loading
	end
end)

There are temperary tool assets in replicated storage though it only has a few of them and they are just simple 4,1,1 parts.

image_2025-02-16_135507231

Do you have different tools located in different locations throughout your game?

no.

Also i have got this error:
13:56:37.406 ServerScriptService.ToolSaving:21: attempt to call missing method ‘Clone’ of string - Server - ToolSaving:21

On these few lines.

	for _, name in ipairs(inventory or { }) do
		local tool = game:GetService("ServerStorage").Inventories.Name
		if not tool then continue end

		tool:Clone().Parent = client.Backpack -- on this line
		tool:Clone().Parent = inventory_folder 
	end
end)

This has to be .Inventories[Name]

I got this error

14:00:12.352 Bottle is not a valid member of Folder “ServerStorage.Inventories” - Server - ToolSaving:18

It might be related to the saving:

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

	for _, item in client.Backpack:GetChildren() do
		table.insert(tools, item.Name)
	end

	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

	player_data:UpdateAsync(key, function(prev)
		print(tools)

		return tools
	end)

	inventory_folder:Destroy()
end)

Use the removing function under this

Nope still got the same error.

This should work fine with 100% no issues (Full 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

Players.PlayerAdded:Connect(function(client)
	local key = "client_" .. client.UserId
	local inventory = player_data:GetAsync(key) 

	for _, name in ipairs(inventory or { }) do
		local tool = game:GetService("ReplicatedStorage")["ItemAssets"][name]
		if not tool then continue end

		tool:Clone().Parent = client.Backpack -- For the player to use
	end
end)

Players.PlayerRemoving:Connect(function(client)
	local key = "client_" .. client.UserId
	local tools = { }

	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)
		end
	end

	for _, item in client.Backpack:GetChildren() do
		table.insert(tools, item.Name)
	end

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

I think I know the reason why the same error pops up.

It is because i haven’t added all tools to the replicatedstoage.itemassets folder

I will do that quickly and lets see if it works!

1 Like

Good news and bad news

Good news:
There is no more errors

Bad news:
My tools don’t load :frowning:

Are all the tools under replicatedstoage.itemassets now? Also can you print out the inventory variable when the player first gets their data?