Stacking a Model on Top of One Another on Touch

*Edit: Previously asked how to clone the models on top of one another. I meant to ask how to move/pivot the models on top of one another without falling.

Hello! I’m trying to create a model that can stack on top of each previous model upon touch without it falling, similar to the mechanics of idle stacking games.

The models are scattered around workspace and if I touch a model it stacks!

I’m able to have one model stack on top of another upon touch, however, I am unsure of how to proceed in stacking the 3rd, 4th, and subsequent models from the position of the previous stacked model.

Could someone please suggest a solution on how I could appraoch this outcome?

Thank you for your help! :smiley:

1 Like

You could keep the last cloned part into a variable, or create a table of stackedup parts, a basic example could be like this, Im creating a model and a part, ignore that just use your model instead of that.

local LastStack

local Cloner = game.Workspace.Cloner

-- New model and part just for testing
	local newModel ="Model")
	local thePart ="Part")
	thePart.Anchored = true
	thePart.Parent = newModel
	newModel.PrimaryPart = thePart
	if LastStack then
		newModel.PrimaryPart.CFrame = LastStack.PrimaryPart.CFrame *,newModel.PrimaryPart.Size.Y,0)	
		newModel.PrimaryPart.CFrame = Cloner.CFrame *,newModel.PrimaryPart.Size.Y,0)	
	newModel.Parent = workspace
	LastStack = newModel -- store the last part in the variable

Hello, one approach you can take to tackle this is by keeping track of what was most recently added to the stack.

local Box = script.Parent

local LastRecord = Box -- Keeps track of what was recetly added to the stack

	local NewBox = Box:Clone()
	NewBox:PivotTo(LastRecord.CFrame + Vector3.yAxis * LastRecord.Size.Y) -- Pivoting the New Item ontop on the previous
	NewBox.Parent = workspace
	LastRecord = NewBox -- Updating the tracking variable