I am trying to rotate a model by certain increments every time the player presses R or F. However, it’s being really weird and rotating in strange patterns that are not at all what I want the increments to be.
In the output logs, the first number that gets printed (5) is the rotationSize. It’s how much I want the model to rotate by. The player would be able to set this on their own. The second number is math.rad(rotationSize), and the third is math.deg(rotationSize).
Neither of these three numbers work in producing the result I want when combined into the following line of code:
I’ve already tried using radians. It just rotates in slightly different but equally as janky ways, unfortunately. Never at the increment I actually ask it to.
Here’s the full part of the UserInputService code that deal with it:
local rotationSize = script.Parent.Parent.PlayerData.RotationSize.Value
if input.KeyCode == Enum.KeyCode.R then
local modelToRotate = script.Parent:FindFirstChildOfClass("Model")
modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(rotationSize),0))
script.Parent.Folder.Orientation.Value = modelToRotate.PrimaryPart.Orientation
end
if input.KeyCode == Enum.KeyCode.F then
local modelToRotate = script.Parent:FindFirstChildOfClass("Model")
modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(-rotationSize),0))
script.Parent.Folder.Orientation.Value = modelToRotate.PrimaryPart.Orientation
end
The script.Parent.Folder.Orientation.Value thing is just a method for storing the orientation data for later. Not important.
Other than what is seen here there’s nothing else that affects the rotation of the model.
Just as a note, I appreciate the quick responses helping me out.
It’s connected just with a simple line of code UserInputService.InputBegan:Connect(function(input, Chatting)
And, to that end, I can confirm it doesn’t fire more than once. Changed my print() functions to just do “Fired” when I press a button, and as you can see in this clip it only fires once per press: 2023-09-21 20-50-17
There’s nothing wrong with the part you posted, my only other guess is that there’s something weird going on with the model or some other part of the script that’s messing that up.
Could you post the whole script / elaborate what the orientation is for?
This is a system for an object placement system. The only part of the code that adjusts the rotation / orientation of the object is as follows:
--KEYBINDS FOR ROTATIONS, ETC
UserInputService.InputBegan:Connect(function(input, Chatting)
if Chatting then return end
if equipped == false then return end
if valid == false then return end
print("Fired")
local rotationSize = script.Parent.Parent.PlayerData.RotationSize.Value
if input.KeyCode == Enum.KeyCode.R then
local modelToRotate = script.Parent:FindFirstChildOfClass("Model")
modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(rotationSize),0))
script.Parent.Folder.Orientation.Value = modelToRotate.PrimaryPart.Orientation
end
if input.KeyCode == Enum.KeyCode.F then
local modelToRotate = script.Parent:FindFirstChildOfClass("Model")
modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(-rotationSize),0))
script.Parent.Folder.Orientation.Value = modelToRotate.PrimaryPart.Orientation
end
end)
Other than what you see here, there is nothing which deals with the orientation of the object. The object’s CFrame changes on RenderStepped based on your mouse position, but that doesn’t touch the rotation and preserves the orientation based on what is registered in the Orientation value I set above:
Euler angles ~= Orientation
It’s a little weird to explain but there’s a really good post that explains it in detail.
Basically all you need to know from it though is that Orientation (for the most part) is Y, X, Z whilst Euler Angles is X, Y, Z, that and some other odd behavior where it flips sign when it hits 180 degrees and stuff and i’m not qualified enough to even know why it exists.
Anyways, all you need to do is either just supply the angles as euler or use CFrame.fromOrientation().
Or even better, since you are already using values, just save the CFrame as a CFrameValue and apply that.