Model rotating on its own without any input

local Rotation = Model.PrimaryPart.Orientation.Y

local function RotateModel()
	if Rotation == 0 then
		Rotation = 45
	elseif Rotation == 45 then
		Rotation = 90
	elseif Rotation == 90 then
		Rotation = 135
	elseif Rotation == 135 then
		Rotation = 180
	elseif Rotation == 180 then
		Rotation = 225
	elseif Rotation == 225 then
		Rotation = 270
	elseif Rotation == 270 then
		Rotation = 315
	elseif Rotation == 315 then
		Rotation = 0
	end

	Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(45), 0))	
end

Mouse.Move:Connect(function()
	if IsMoving then
		Mouse.TargetFilter = Model
	
		local PosX, PosY, PosZ = Snap(Mouse, Model)
		
		for i, v in pairs(Model:GetChildren()) do
			v.CanCollide = false
		end
		
		local UnitRay  = Camera:ScreenPointToRay(Mouse.X, Mouse.Y, 1)
		local NewRay = Ray.new(UnitRay.Origin, UnitRay.Direction * 1000)
		--local Hit, Pos, Normal = workspace:FindPartOnRay(NewRay, Model)
		local Hit, Pos, Normal = workspace:FindPartOnRayWithIgnoreList(NewRay, {Model, Player.Character})
		
		Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * CFrame.Angles(-math.rad(90), math.rad(Rotation), 0) * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))
	end
end)

For some reason my model decides to rotate automatically when being moved. When ever I move it it rotates 90 degrees.

Note as well, as seen in the gif, when the UI disappears, so does this script (gets destroyed)

com-video-to-gif

So everytime I click on the model, this UI gets created, so it’s a new script (that’s why I did local Rotation = Model.PrimaryPart.Orientation.Y at the top instead of 0, not sure if that affects anything)

The green button is to move btw, not rotate. Pressing the green button should not rotate this model in anyway

1 Like

The code you posted never actually uses the RotateModel function (which could be significantly shortened with Rotation = (Rotation + 45)%360) so it looks like the cause of the problem is something that you’ve left out of the post.

I only call the function when the button is being pressed

Rotate.Activated:Connect(function()
	RotateModel()
end)

Which the button isn’t being pressed in the video example

Did you check whether that event is being fired? If that event being fired is the only way for the model to be rotated, and the model is being rotated, then that event is being fired. What is Rotate?

Rotate is just the button.

The event isn’t being fired at all, so my thought it is has something to do with this

local Rotation = Model.PrimaryPart.Orientation.Y

Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * CFrame.Angles(-math.rad(90), math.rad(Rotation), 0) * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))

My guess is that the default Rotation value isn’t right? Not sure how or why.

It could be the case that the orientation is being represented differently by flipping the axes, so just reading the Y orientation is not enough information to see how it is rotated on the Y axis. Instead of storing the rotation as a number, you can just store its CFrame minus its position, and then translate that value when changing the rotation instead of changing a number.

Not exactly sure what you mean there??

Dun some more testing (even removed the rotating function completely, still occurs)

local Rotation = Model.PrimaryPart.Orientation.Y
print(Rotation)

So ye, everytime I click off the model and reclick, it just rotates itself, so I’m sure it has something to do with the Model.PrimaryPart.Orientation.Y

I’ve taken a step kinda back to get it in better working condition (how I had it originally)

local Rotation = 0

local function RotateModel()
	if Rotation == 0 then
		Rotation = 45
	elseif Rotation == 45 then
		Rotation = 90
	elseif Rotation == 90 then
		Rotation = 135
	elseif Rotation == 135 then
		Rotation = 180
	elseif Rotation == 180 then
		Rotation = 225
	elseif Rotation == 225 then
		Rotation = 270
	elseif Rotation == 270 then
		Rotation = 315
	elseif Rotation == 315 then
		Rotation = 0
	end
	
	Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(45), 0))
end

-- Move object
Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * CFrame.Angles(-math.rad(90), math.rad(Rotation), 0) * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))

This is how I had it originally, and it worked perfect. The problem I found with this tho was when you unselected the model and reselected it and moved, it would rotate back to 0 because of this

Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * CFrame.Angles(-math.rad(90), math.rad(Rotation), 0) * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))
-- and with Rotation = 0 at the top of the script

So that’s why I tried Model.PrimaryPart.Orientation.Y to hopefully ‘retain’ the actual rotation instead of it being 0, but that just broke it completely :confused:

Question still unsolved for anyone who has any advice :grimacing:

Model.PrimaryPart.Orientation.Y is not the correct way to retrieve the rotation. 3D rotations have multiple representations and you are assuming it takes the form you want where it only uses the X and Y axes, but when it doesn’t, the Y component is not the rotation you are looking for. Instead of trying to figure out what it should be, just have local Rotation = Model.PrimaryPart.CFrame - Model.PrimaryPart.Position and

local function RotateModel()
	Rotation = Rotation * CFrame.Angles(0, math.pi/2, 0)
	Model:SetPrimaryPartCFrame(Rotation + Model.PrimaryPart.Position)
end

and Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * Rotation * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))

1 Like

Hmm ok. Just quick point, I can’t do

Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * Rotation * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))

I’d need at least * CFrame.Angles(-math.rad(90), 0, 0) as the model is rotated (for some reason?) and needs to be corrected to stand up right

Before moving


After moving

But doing

Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * Rotation * CFrame.Angles(-math.rad(90), 0, 0) * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))

Didn’t change anything