How to get all parts in a model to follow PrimaryParts rotation?

Wait, are you just trying to rotate a model 90 degrees? If so then all you need is my second function and pass in CFrame.angles(0, math.pi/2, 0) along with your model. If you are rotating/translating a part, then want to move all the rest of the parts around the rotated/translated part then use the first function to get the rotation to pass into the second.

No, the model shouldn’t rotate whatsoever. But when I click on the model i call this function like this:

initiatePlacement(hit.Parent.Name, hit.Parent.PrimaryPart.Rotation, false)

function initiatePlacement(itemName, rotation, placing)
	cancelPlacement()
	
	dummyModel = items[itemName]:Clone()
	if rotation then
		dummyModel:SetPrimaryPartCFrame(CFrame.new(dummyModel.PrimaryPart.Position) * CFrame.Angles(rotation.X, rotation.Y, rotation.Z))
	end
end

And that is just the part of the function that sets the primary part of the model. But for some reason when I click the model it automatically just rotates it 180 degrees for no apparent reason

But wait… if the model shouldn’t rotate, then what does it mean to get all parts in a model to follow PrimaryPart’s rotation? Like each part individually rotates, but not about the primary part? If so then replace my second function with this one:

local function applyRotation(rotation, model)
	for i, desc in next, model:GetDescendents() do
		if desc:IsA 'BasePart' then
			desc.CFrame = desc.CFrame * (rotation + desc.CFrame.p)
		end
	end
end

Because the primaryparts orientation determines the models rotation. I have more code that basically rotates the model when you press R, but clicking on the model itself should just pick up the model, not rotate it 180

What do you mean by ‘pick up the model’?

Clicking on the model picks it up, so you can move it around. I excluded all of that because it was unnecessary and the calling of the function as well as the function itself are all that’s required in the question

If you clone the model, it will clone the original model’s rotation. Have you tried taking out the rotation code? Making it look like this:

function initiatePlacement(itemName, _, placing)
	cancelPlacement()
	dummyModel = items[itemName]:Clone()
end

The reason I need the rotation stuff is because items[itemName]:Clone() gets the model that’s stored in repstorage, which I use for placing items. But if a player has already placed the item with a different rotation to the model inside the storage then when they click on it to move it, it gets rotated back to the original rotation of the model being cloned. I wanted the rotation so when you clone the original model from repstorage it gets given the same rotation as the model you had placed before

Here’s a quick video to show what I mean incase you’re getting a little confused. All I am doing is clicking on the models. Not rotating them. They automatically just rotate 180 degrees on their own.

robloxapp-20180822-2239321

Ignore the later half of the video where the model rotates a lot, that was me actualy pressing R. But the first few times of me just clicking it was what I mean. It shouldn’t just be rotating 180 degrees for no reason.

Okay, then call newmodel:SetPrimaryPartCFrame(oldModel:GetPrimaryPartCFrame())

Why do you need a new model from the replicated storage instead of just selecting the one currently in the workspace?

Because the code eventually calls a remote event to the server, which is what gets the model from the repstorage and places it properly. If I had 100 items with the same name inside workspace then the serverscript would just pick the first one instead of it being the actual one i clicked.

You can pass instances over the remote event. You can directly tell the server which model you clicked on. Each entity in the world has a unique ID internally so events can get replicated from the server to the client, and the client can send instance IDs back over to the server. That way you can avoid all of this. ^.^ But my solution above should work anyways.

if rotation then
	dummyModel = items[itemName.Name]:Clone()
	dummyModel:SetPrimaryPartCFrame(itemName:GetPrimaryPartCFrame())
	currentItemName = itemName.Name
else
	dummyModel = items[itemName]:Clone()
	currentItemName = itemName
end

Just tried using :GetPrimaryPartCFrame() and it still rotates them 180 degrees :confused:

Unfortunately I don’t know what the items dictionary is, nor itemName. I’d need more to debug it. Is itemName always the old model? Why is it in an if statement? It should always have the same rotation.

itemName is the model itself (so itemName.Name is the items name)

I suspect this might be to do with the rotation part of your placement code, ie when you have an item you are moving the way you can rotate it deliberately. It’s easy to have the current rotation set to a global which re-rotates when you select something new rather than being reset to 0.

Also you would want the CFrame of the object you are clicking on, not the version of it in items.

As a side note, you may also be interested in the Dragger service. Dragging parts is a common enough operation they decided to make a service for it ^.^

Ok I used this:

dummyModel:SetPrimaryPartCFrame(itemName:GetPrimaryPartCFrame():inverse() * dummyModel:GetPrimaryPartCFrame())

and for some reason, it doesn’t rotate the model if the model’s rotation is either 90 or -90 (Y), but when it’s 0 or 180 it doesn’t work.

For reference, the 4 rotations of the primary part are 0, 90, 180, -90 (I don’t know why -90, blame roblox XD)

Are you sure itemName is always the correct model? If you try removing it in the initialize position function, does it remove the right model? Is it possible that you are setting the old model’s Primary Part’s CFrame elsewhere, causing it not to be the same as the one stored in items? Does it work when you have placed a model but never rotated it? Can we see the rotation code?