Cloning an object to a Viewport causes all children to be deleted

This is an annoying issue I have been trying to solve for a while, with no luck.

I am creating a custom inventory sytem, and it requires me to clone objects/tools to display them in a Viewport.

            local item = game.ReplicatedStorage.PlayerInventories:FindFirstChild(Player.Name).Tools:FindFirstChild(tool.Name, true):Clone()
			item.Parent = toolButton.ViewportFrame
			local itemCamera = Instance.new("Camera")
			itemCamera.Parent = toolButton.ViewportFrame
			itemCamera.CFrame = item.PrimaryPart.CFrame + Vector3.new(-0.027, -0.05, 3.155)
			itemCamera.CFrame = CFrame.lookAt(itemCamera.CFrame.Position, item.PrimaryPart.Position)
			itemCamera.CFrame = itemCamera.CFrame * CFrame.Angles(0, 0, math.rad(45))
			toolButton.ViewportFrame.CurrentCamera = itemCamera

For some reason, when it clothes the item and then parents it to the viewport frame, it results in that object’s children (i.e. scripts, handles, parts, everything) to be deleted or just not carried along with it. I’ve tried about 10 different ways of cloning the item, to no avail.

Seems to only (or mostly only) happen to tools.

1 Like

How do you know it is deleted? did you check under viewports? You said that it happen to tools more often , how many parts are their in the tool or is it just handle?

1 Like

Yeah, I look in the viewport. In case the explorer in the studio is wrong, for whatever reason, I also run:

for _, part in ipairs(item:GetChildren()) do
     print(part.Name)
end

and it returns nil. (this is just an example, i don’t have studio open at the moment.)

The number of parts in the tool is anywhere between one (just a handle) to several depending on the complexity of the tool. Either way, it doesn’t transfer over properly.

1 Like

sorry for the late response but it might be a fault in other areas of your script. Does the inventory system use a viewport to display one tool ?

1 Like