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

dummyModel.PrimaryPart.Rotation = rotation

Moving the part

Part placed back

As you can see in the first image, the faintly visible black part is the PrimaryPart, and it’s been rotated correctly, however, the other part inside the model doesn’t follow that some rotation. Any advice?

Rather than setting rotation, learn to use CFrames, they can take you further.
After that, give this a go.

model:SetPrimaryPartCFrame(
    CFrame.new(model.PrimaryPart.Position)
    * CFrame.Angles(X, Y, Z)) -- X Y Z are in radians. Use math.rad to convert.

That should keep the position and then set the rotation.

Both CFrame.Angles and your current rotation method are both kinda iffy in the way they work, as soon as you learn more about CFrame rotations I suggest migrating away from XYZ (aka Euler) rotations.

2 Likes

How would I send it through as an argument?

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

function initiatePlacement(itemName, rotation, placing)
	cancelPlacement()
	
	if rotation then
		dummyModel = base:WaitForChild('ItemHolder'):FindFirstChild(itemName)
		dummyModel.PrimaryPart:SetPrimaryPartCFrame(CFrame.new(dummyModel.PrimaryPart.Position) * CFrame.Angles(rotation))
end
end

Number expected, got no value (for Angles)

Rather than sending rotation as a Vector3, you need to break it into the X Y and Z components:

function initiatePlacement(itemName, rotation, placing)
    cancelPlacement()
    if rotation then
        dummyModel = base:WaitForChild('ItemHolder'):FindFirstChild(itemName)
        dummyModel.PrimaryPart:SetPrimaryPartCFrame(CFrame.new(dummyModel.PrimaryPart.Position) * 
        CFrame.Angles(rotation.X, rotation.Y, rotation.Z))
    end
end
1 Like

Ok it seems to be working alright, just minor bug.



When I move the item it rotates 180 degrees each time.

initiatePlacement(hit.Parent.Name, hit.Parent.PrimaryPart.Rotation, false) -- Fired when mouse clicks on the model

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

The function. I don’t see why or any reason as to why click it automatically just rotates it 180 degrees

Terribly sorry, I goofed on my last reply. You also need to convert to radians.

* CFrame.Angles(math.rad(Rotation.X), math.rad(Rotation.Y), math.rad(Rotation.Z))
1 Like

Still rotates them 180 degrees for some reason, refer to previous post of mine with images.

Type using these functions. Use the first to find the rotation needed to turn the part you are placing with the old version of the part. Then pass that rotation result into the second function to rotate the entire model. Note this code isn’t tested, I can’t be blamed if your workspace blows up!

local function getRotation(oldCFrame, newCFrame)
	return oldCFrame:inverse() * newCFrame
end

local function applyRotation(rotation, model)
	local cf = model:GetPrimaryPartCFrame()
	model:SetPrimaryPartCFrame(cf * (rotation + cf.p))
end

Edit: This should also correctly handle translation.

after a quick overlook, setting the cframe should work
are you sure it’s not a problem with the given “rotation” value? how do you determine “rotation”?

hit.Parent.PrimaryPart.Rotation

How would I implement that into what I’ve already got tho?

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.