How do I get this loop to leave Clone() in each model?

I’m having trouble with a table operation, loop, where I want to clone an object from ReplicatedStorage into multiple models in the workspace… I have the cloning process and how many I need figured out, but I can’t figure out how to drop them into each model. I also have to position the objects within each model so basically im trying to just loop each new (cloned) object into each model and have it position them too…

One table is the “mail boxes” (workspace models)
The other table is the objects I want to put in (11 new Clones)

First I Clone the object as many times as needed then distribute them into each separate model? Is this even the right path to take for this?

What it does now is clone everything but puts them all into the last workspace model in the table, instead of evenly distributing them into each model in the table

local Boxes = workspace.PaperRoute.Boxes
local newBoxes = {}

local addGoals = Boxes.Small:GetChildren()
	for index = 1, 11 do 
	local newGoal = goal:Clone()
	table.insert(newBoxes, #newBoxes + 1, newGoal)
end
	for index, newGoal in ipairs(newBoxes) do
	newGoal.Parent = addGoals[#addGoals]
	newGoal.CFrame = CFrame.new(addGoals[#addGoals].PrimaryPart.Position)
	newGoal.Rotation = Vector3.new(addGoals[#addGoals].PrimaryPart.Orientation)
end

I’ve tried doing it with a “repeat” but no luck…

Hi!

You are obviously on the right track. The goal is to loop through every new box and replicate it in particular set of mail boxes. You can simplify this by leaving out table inserting, which is not actually needed.
I am a little hesitant with putting the code here, because I haven’t had a chance to test it, as my empty BasePlate has no houses and mailboxes. :slight_smile:

local Boxes = workspace.PaperRoute.Boxes:GetChildren()
local NewBoxes = Boxes.Small:GetChildren()

for index = 1, #NewBoxes do
	local newGoal = goal:Clone()
	newGoal.CFrame = CFrame.new(Boxes[index].PrimaryPart.Position)
	newGoal.Rotation = Vector3.new(Boxes[index].PrimaryPart.Orientation)
	newGoal.Parent = Boxes[index]
end

So you have a folder or model called Boxes in Workspace, that contains all the mail boxes you want the goal delivered to. Get children stores a list with all of them. Now, also list every small box, which you want to deliver, in NewBoxes. In the code above, goal/package is not yet defined and doesn’t yet have a path. Anyway, you can create a new clone every time. The old clone is copied in Workspace, but it’s reference is overwritten by the new one’s. Boxes[index] represents the first child in the folder/model. Apply new properties to the created clone and let it keep getting redefined through each pass. When for-loop ends, newGoal is not referenced anymore, because it only had reference inside the block, so it can not be cleared.

Here comes another problem we may think of. It is alright if there are more mail boxes than there are packages. But what if it’s the opposite? Error occures. Here’s where metatables can come in very good use.

Again, the concept has to work, but not necessarily the above code. If you have any further trouble, you can either send me a private message or a direct message on Discord (username in my profile description).

1 Like