Data store with tools not working

Yes all item assets are in replicatedstorage.

Here’s my output when i leave the game
image_2025-02-16_142510780

I meant print out this

I need to see if it’s an empty table

like this?

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

like this:

Players.PlayerAdded:Connect(function(client)
	local key = "client_" .. client.UserId
	local inventory = player_data:GetAsync(key) 
	print(inventory) --This the print
	
	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)

It is in fact not an empty table!

image_2025-02-16_143327508

Switch to this:

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

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

Ok now when i join the game the table prints out
image_2025-02-16_143821014

Except those items aren’t in my backpack.

Did u use the updated loop? It also prints stuff can you tell me what they are

I used the updated loop and the only thing that happens is that the table prints out thats it.

Can you remove this line to see if it errors?

no errors

Also i have to go for a bit.

Still type solutions bc I will try them once in a while.

To be honest, I’m not really sure too… Maybe next time try printing the tool that’s getting cloned.

Like this:

for _, name in ipairs(inventory or { }) do
		local tool = game:GetService("ServerStorage")["Tools"][name]
		print(tool)

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

I think I know why because tool is a string not a instance.

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

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

Because when I print tool it prints out a string instead of an error.

OMG I GOT IT TO WORK!!!

What i did was inside

Players.PlayerAdded:Connect(function(client)

I also did

client.CharacterAdded:Connect(function(plr)

And that got it to work!
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)
	client.CharacterAdded:Connect(function(plr)
		local key = "client_" .. client.UserId
		local inventory = player_data:GetAsync(key) 
		print(inventory)

		for i, name in inventory or {} do
			local tool = game:GetService("ReplicatedStorage").ItemAssets:FindFirstChild(name)
			local Backpack = client.Backpack

			if tool then
				print(tool.Name)
				local Clonedtool = tool:Clone()
				Clonedtool.Name = tool.Name
				Clonedtool.Parent = Backpack
				print("tool cloned")
			else
				warn("Item could not be cloned")
			end
		end
	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)

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