Cloned tool's children returns 0

I tried to print the uncloned tool before I called the function and it is printed in the output

Ah my bad, I didnā€™t see the greater than 0 part.

I tried but the model doesnā€™t have any children

Something is wrong with your tool or something, I tried running the script in my Roblox Studio and it worked perfectly:

image
image

Hereā€™s my script:

local plrs = game:GetService("Players")

local function clone_tool_model(tool : Tool)
	local cloned_tool = tool:Clone()
	local model = Instance.new("Model")

	repeat task.wait() warn("Children <= 0") until #cloned_tool:GetChildren() > 0

	for _, obj in pairs(cloned_tool:GetChildren()) do
		obj.Parent = model
	end

	cloned_tool:Destroy()
	return model
end

plrs.PlayerAdded:Connect(function(plr)
	repeat task.wait() until plr.Backpack:FindFirstChild("Tool")
	local model = clone_tool_model(plr.Backpack:FindFirstChild("Tool"))
	model.Parent = workspace
end)

I only got 1 warning:
image

This is my portion of code that should be related to the bug:

local function clone_tool_model(tool : Tool)
	local cloned_tool = tool:Clone()
	local model = Instance.new("Model")
	
	repeat task.wait() warn("Children <= 0") until #cloned_tool:GetChildren() > 0
	
	for _, obj in pairs(cloned_tool:GetChildren()) do
		obj.Parent = model
	end
	
	cloned_tool:Destroy()
	return model
end

backpack.ChildAdded:Connect(function(child)
	if least_possible <= inventory_size - 1 and not pressing and not inventory_storage[table.find(inventory_storage, child)] then
		least_possible += 1
		inventory_storage[least_possible] = child
		
		--clear_inventory()
		
		for _, tool in pairs(backpack:GetChildren()) do
			local tool_name = tool.Name
			local tool_index = table.find(inventory_storage, tool)
			
			local slot = slot_background[tool_index]

			--slot.item_name.Text = tool_name
			slot.item.Value     = tool_name
			
			local tool_image  = slot.tool_image
			local world_model = tool_image.WorldModel
			
			world_model:ClearAllChildren()
			
			print(tool)
			
			local tool_model = clone_tool_model(tool)
			tool_model.Parent = world_model
			tool_model:PivotTo(CFrame.new(0,0,0))
			
			local camera  = Instance.new("Camera")
			camera.Parent = tool_image
			tool_image.CurrentCamera = camera
			
			camera.CFrame = CFrame.new(0, 0, -3)
			camera.CFrame = CFrame.new(camera.CFrame.Position, Vector3.new(0,0,0))
		end
	end
end)

Is the toy knife the only tool in the backpack? It might loop to check a different tool instead, yielding the code because youā€™re calling a function, and the different tool might have 0 children.

The backpack had only 1 tool

Sorry Iā€™ll be having dinner now, Iā€™ll get back asap

You should try checking if the toolā€™s name is ToyKnife anyways.

Alright, hereā€™s what I could gather. Using backpack.childadded didnā€™t run for me (could perhaps be one of the reasons itā€™s giving you this warning countless times), so I used character.childadded and it worked. I removed most of the lines of code because they were using variables that I had no clue what they were. Hereā€™s my code:

local plrs = game:GetService("Players")

local function clone_tool_model(tool : Tool)
	local cloned_tool = tool:Clone()
	local model = Instance.new("Model")

	repeat task.wait() warn("Children <= 0") until #cloned_tool:GetChildren() > 0

	for _, obj in pairs(cloned_tool:GetChildren()) do
		obj.Parent = model
	end

	cloned_tool:Destroy()
	return model
end

plrs.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()

	char.ChildAdded:Connect(function(child)
		if child:IsA("Tool") and child.Name == "ToyKnife" then
			for _, tool in pairs(char:GetChildren()) do
				local tool_name = tool.Name
				if tool:IsA("Tool") and tool_name == "ToyKnife" then
					print(tool)

					local tool_model = clone_tool_model(tool)
					tool_model.Parent = workspace
					tool_model:PivotTo(CFrame.new(0,0,0))
				end
			end
		end
	end)
end)

I am back, I need to use Backpack.ChildAdded in order for my inventory code to work though.

Also I tried to print the length of the children of the uncloned tool under the loop and it returned 0.

Also this seems like a server script, the problem was occurred from a local script (might not change so much)

The solution is pretty simple, you have to yield the script until everything loads.

I rewrote some scripts and it worked, Iā€™ll still mark your answer as the solution so as to help anyone with a similar problem

1 Like

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