Tool disappearing when parented to backpack

  1. What do you want to achieve?
    Im making a datastore script and using this method to check which tools a player has and then putting them in their backpacks

  2. What is the issue?
    The tool that is being transferred to the backpack is getting its parent set to nil for some reason

  3. What solutions have you tried so far?
    I found out that the tool gets transferred to the backpack fine when when the character has loaded in so if I wrap the for loop in a characteradded event or just add a wait before it then it does work. I would rather have the data loaded as soon as the player joins though.

for k, v in pairs(playerData["Items"]) do
	local tool = game.ServerStorage.Items:FindFirstChild(k)
	if tool then
		print("tool found")
		local playerTool = tool:Clone()
                playerTool.AncestryChanged:Connect(function(tool, parent)
			print(parent)
		end)
		playerTool.Parent = plr.Backpack
		
		local invClone = playerTool:Clone()
		invClone.Parent = items
		invClone:SetAttribute("Amount", v)
		
	end
end

--Output from ancestrychanged event
--Backpack
--nil(x2)

The reason it is working when the character loads in is that every time a new characer is created, the backpack gets refreshed (destroyed, emptied, whatever the technical term is). Changing plr.Backpack to plr:WaitForChild(“Backpack”) would most likely solve your issue.

nope, same thing happens. About your point about the backpack refreshing, do you think that initially, the tool is in the backpack(as it says in output) and then when the charchter spawns in, the backpack gets refreshed so the tool dissappears?

If you are only calling this code when the player first joins, then yes it is because when the player spawns the backpack is getting refreshed, which is why when you connected it to the CharacterAdded event it worked for you.

so i would have to implement the charachteradded event, do you think a wait might be better though incase the data hasnt loaded yet and the event gets called

nvm i can just parent it to startergear

1 Like