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

Cloner.ClickDetector.MouseClick:Connect(function()
-- New model and part just for testing
	local newModel = Instance.new("Model")
	local thePart = Instance.new("Part")
	thePart.Anchored = true
	thePart.Parent = newModel
	newModel.PrimaryPart = thePart
	
	if LastStack then
		newModel.PrimaryPart.CFrame = LastStack.PrimaryPart.CFrame * CFrame.new(0,newModel.PrimaryPart.Size.Y,0)	
	else
		newModel.PrimaryPart.CFrame = Cloner.CFrame * CFrame.new(0,newModel.PrimaryPart.Size.Y,0)	
	end
	
	newModel.Parent = workspace
	LastStack = newModel -- store the last part in the variable
	
end)

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


Box.Touched:Connect(function()
	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
end)