Tool doesn't clone to player's backpack

Issue: I’ve scripted this code that you can see below for cloning a tool into the player’s backpack, although it doesn’t clone at all. Even when I printed the location of the cloned tool, it was in the players backpack, but there was nothing inside of there. I’m really confused and hope to find the issue here on the forum

local function copyItems(plr)
	local playerItems = module.playerItems(plr)
	
	for _,statvalue in pairs(playerItems) do
		if(ssItems:FindFirstChild(statvalue) ~= nil) then
			local foundItem = ssItems[statvalue]:Clone()
			foundItem.Parent = plr.Backpack
			print(foundItem.Parent.Parent.Parent)
		else
			warn("Didn't find", statvalue, "in the workspace")
		end
	end
end

Background info: the copyItems function is triggered when a player joins and on update.

game.Players.PlayerAdded:Connect(function(plr)
	local Stats = plr:WaitForChild("Stats")
	local Level = Stats:WaitForChild("Level")
	local Items = Stats:WaitForChild("Items")
	
	copyItems(plr)
	
	Level:GetPropertyChangedSignal("Value"):Connect(function()
		healthSetting(plr, Level)
		print(module.maxExp(plr, Level))
	end)
	
	Items:GetPropertyChangedSignal("Value"):Connect(function()
		copyItems(plr)
	end)
	
--	local items = module.playerItems(plr)
--	module.addPlayerItem(plr, "5")
end)

playerItems is a table full of strings with ID’S of the player’s item’s
ssItems is a folder with all the tools located in ServerStorage

Hope you guys can help me!

5 Likes

if your looking in studio go to the player under the players field and look in that backpack.

That’s what I was doing, still couldn’t find the clone.

can you post module.playerItems function? You called it in your copyItems function. And please don’t post screenshots of the code, it can be hard to read from a screenshot, and impossible for us to copy so we can reproduce your problem, instead directly paste the code into the body of your thread, in code block

```lua
– code here
```

Maybe you are not indexing the tables properly. When you run the code does it constantly end up printing “Didn’t find …”, from your copyItems function?

I’ve checked if it was the module.playerItems function’s fualt this was happening before, and it wasn’t but sure, here you have it!

function module.playerItems(player)
		print("Start")
		local Stats = player:WaitForChild("Stats")
		local pID = Stats:WaitForChild("Items")
		
		local playerItems = {}
		local allItems = {
			['1'] = "Beginner Sword",
			['2'] = "Beginner Armor",
		}
		for a in pID.Value:gmatch("%w+") do
			local item = allItems[a]
			if(item) then
				table.insert(playerItems, item)
			else
				warn("ID:", a, "was not found when searching", player.Name, "'s inventory")
			end
		end
		print(table.concat(playerItems, " "))
		return playerItems
	end

image
When using .Value

1 Like

Nope, it doesn’t. No errors in the output!

1 Like

A developers favorite output. Okay, can you screen shot the directory of the ssItems?

image and
image

Thanks. Can you print out the length or contents of “playerItems” table immediately after the module function?

It’s already in the script above.

Ahh okay, I missed that. And it outputs properly?

Yes image

I don’t think you can access the object in the ssItems folder by treating it as a table.

for _,statvalue in pairs(playerItems) do 
    if(ssItems:FindFirstChild(statvalue) ~= nil) then 
        local foundItem = ssItems[statvalue]:Clone()

try this

for _,statvalue in pairs(playerItems) do 
    local foundItem = ssItems:FindFirstChild(statvalue)
    if foundItem then 
        local clonedItem = foundItem:Clone()

maybe I’m wrong but worth a shot

edit: yep i’m wrong.

You should try to use plr.CharacterAdded:wait() before you parent the item to the players backpack there is a bug that if the character is not loaded it will not put it correctly in the backpack from what I have ran into and tested in the past

I have also Isolated why this is happening. When the tool is added before the Character it is actually being added to the backpack but when the Character becomes part of the game it sets all parents of backpack items to nil

10 Likes

Yeah that’ll work but I often keep the lines as tidy as I can lmao, and it still doesn’t work…

1 Like

:clap: good to know. Nice catch

1 Like