Setting new parent problems

-- Client
CustomiseHolder.Accessories.ChildAdded:Connect(function(accessory)
	accessory.Parent = Dummy
	accessory:Destroy()
end)

-- Server
RetrieveItem.OnServerEvent:Connect(function(player, assetType, id)
	local Item = InsertService:LoadAsset(id)
	local Accessory = Item:GetChildren()[1]
	local CurrentFolderItem = CustomiseHolder:FindFirstChild(assetType)
		
	if assetType == 'Hats' or assetType == 'Hairs' or assetType == 'Accessories' then
		Accessory.Parent = CustomiseHolder.Accessories
	end
	
	Item:Destroy()
end)

Basically the server creates the item, and puts it in a folder in RepStorage. The client is then suppose to detect the accessory being added, and then parent it to an NPC, however, I get this warning:

10:44:04.830 - Something unexpectedly tried to set the parent of Ultra-Fabulous Hair Brown to Dummy while trying to set the parent of Ultra-Fabulous Hair Brown. Current parent is Accessories.

10:44:04.830 - Something unexpectedly tried to set the parent of Ultra-Fabulous Hair Brown to NULL while trying to set the parent of Ultra-Fabulous Hair Brown. Current parent is Accessories.

So the item stays in the folder instead of going to the npc (dummy)

Race condition. You’re changing parents and calling destroy too quickly. If the warning is coming from the client-side, it has to do with you calling destroy on the accessory right after changing its parent. Why is that necessary?

1 Like

Even if I remove the :Destroy() I still get a warning

If you are trying to move the child that just got added, you need to yield the thread in a ChildAdded handler at least once. A single wait() statement at the beginning should suffice.

Also, I don’t get why you are reparenting a just-replicated item and then destroying it. That makes no sense. You might as well just destroy it.

Did you mean to clone the accessory?