Placement Script Rotation isn't working properly

Hey everyone!

So I’m still working on my Placement System and I just can’t get this Rotate function right.

Here’s my code:

function _PS:RotateModel()
	print("RotateModel() called.")
	for _, part in pairs(PreviewModel:GetChildren()) do
		local newOrientation = part.Orientation + Vector3.new(0, 90, 0)
		part.Orientation = newOrientation
	end
end

and here’s what happens:

I can rotate it, but when I move my mouse or try to place, it rotates back.

Thanks! :slight_smile:

You only have rotated the part inside the PreviewModel, but anyways, I don’t know how your placement system works, so it’d be cool if you provide us with more information

When you place it, the wall’s information will be transferred to the server, including rotation.

that might be the problem, can you please show us the script, because the problem is in when you place the actual wall

It also resets when I move my mouse.

Here’s the code, nonetheless.

local Buildables = game.ReplicatedStorage.Buildables

function _PS:Place(playerName, build, position, rotation)
	assert(Buildables:FindFirstChild(build) ~= nil, "Couldn't find Build in Buildables folder.")
	local player = game.Players[playerName]
	local Build = Buildables[build]:Clone()
	Build.Parent = game.Workspace["Player Builds"]
	Build:SetPrimaryPartCFrame(CFrame.new(position))
	Build.PrimaryPart.Orientation = rotation
end

change this: Build:SetPrimaryPartCFrame(CFrame.new(position)) to this:

Build:SetPrimaryPartCFrame(
	CFrame.new(position) * CFrame.Angles(
		math.rad(position.X), 
		math.rad(position.Y), 
		math.rad(position.Z)
	)
)

That rotates it to be a floor.

i mean, you did already set the cframe to the preview’s cframe, I don’t see the needing of changing the orientation of it

since CFrame is position + orientation

Still resets it when I place, and when I move my mouse.

then it must be related to when you move your mouse, click it, can you show us that part of script too :stuck_out_tongue:

Here’s when I update it (move my mouse):

function _PS:UpdatePreviewModelCFrame()
	if not IsBuilding then return end
	if PreviewModel == nil then warn("Preview Model nil.") return end
	CanPlace = not _PS:CheckForOtherBuilds()
	local UnitRay = mouse.UnitRay
	local Range = 30
	local RayCast = Ray.new(UnitRay.Origin, UnitRay.Direction * 100)
	local _, pos = workspace:FindPartOnRay(RayCast, PreviewModel)
	PreviewModel:SetPrimaryPartCFrame(CFrame.new(pos))
end

and the placement just sends a remote event, which is then transferred through to the other script I sent you.

PreviewModel:SetPrimaryPartCFrame(CFrame.new(pos))

That’s the line, because it changes PreviewModel frame to pos.
Can you show us the script with pos please

pos is determined above the line you’re looking at.

What’s the target of the Raycast? the Wall or the part inside the PreviewModel?

The target? I’m not too familiar with Rays. I set the UnitRay variable to the mouse.UnitRay property.

Do PreviewModel is handled through LocalScript or Script?
If it’s handled in Client, the server can’t see the rotation.

It’s on the client, but when placing, the server is told about the rotation, position, etc…

So you should try the most basic debbuging method, use print(part.Orientation), in Server and Client. I also think you’re only changing the Orientation of PreviewModel, and not the “Model” itself.

I did it, and they’re lined up, but the actual build doesn’t rotate.

Also, I did try rotating the actual model. Didn’t work.

PreviewModel:SetPrimaryPartCFrame(CFrame.new(pos))

Well atleast now we know that this part is the cause of your orientation resetting because this is the only line of script that changes the preview model when you move or click.
somehow the pos is the previewmodel’s CFrame without the orientation, maybe it hit another part or something?