Part not rotating as intended

I have a lever in my game that rotates everytime a character touches it
It worked but when I decided to use collectionservice instead to keep things clean the lever now rotates like this:


When it should rotate like this:

I tried many things to fix the issue like using
CFrame.fromEularAnglesYXZ
CFrame.Angles
I tried with and without math.rad
Adding 60 degree to the lever(Not resetting its current rotation)

It seems like there is something I don’t know about CFrames that makes it rotate like this
Also When I change Rotation of the lever(a part in the lever model not the whole model)
from (60,180,0) to (120,180,0) It rotates as i want it but orientation changes to (60,0,-180)
I have no idea why this happens

Here is the current script:

local CollectionService = game:GetService("CollectionService")

local Debounce = {}

for i,v in CollectionService:GetTagged("Lever") do
	v:WaitForChild("Lever")
	v.Lever.Touched:Connect(function(hit)
                --V is the model and v.lever is the cylinder in the lever
		if hit.Parent:FindFirstChild("Humanoid") and not Debounce[v] then
			
			local LeverHandle = v.Handle

			local Angle = CFrame.fromEulerAnglesYXZ(math.rad(60),0,0)
			
			LeverHandle.CFrame = CFrame.new(LeverHandle.Position) * Angle
			Debounce[v] = true
		end
	end)
end

I would really appreciate any help or any piece of information that could help
Tell me if anything isn’t clear

I think the orientation u used (LeverHandle.CFrame = CFrame.new(LeverHandle.Position) * Angle) isn’t correct, wich explains the problem you’re encountering.

Can you tell me in a more clear way how to fix the issue?
btw I tried (LeverHandle.CFrame = LeverHandle.CFrame:ToWorldSpace(Angle)) before that

I ain’t very good using mathematics in programming, so maybe ask chatgpt (sometimes he’s super useful)

Thanks for the help I might try chatgpt although I don’t like using ai at all when it comes to scripting

yeah but i recommand you using it for simple thing, but i think chatgpt can handle math a lil bit

btw sorry if I couldn’t help you more, it’s because I only started coding 6 months ago, so I still lack a lot of knowledge :sweat_smile:

I hope someone else will be able to help you!

Anyone else can help me fix this?

Why reinventing the wheel?

	
LeverHandle.CFrame = CFrame.new(LeverHandle.Position) * CFrame.fromEulerAnglesYXZ(math.pi/3,0,0)

TL;DR you are setting rotation to be 60,0,0 in degrees

So basically i need to add 60 degree to the rotation? If this is the case then this doesn’t work already tried it

local Angle = CFrame.Angles(math.rad(60),0,0)
LeverHandle.CFrame = LeverHandle.CFrame:ToWorldSpace(Angle)
--Or
LeverHandle.CFrame *= Angle

Edit:btw sry for not telling you that

Can you show us the original position, the intended position and the results?

You could try adding an invisible model of the lever so when activated you just tween the cframe making it smooth and more accurate

The intended position and the results in the post I will take a screenshot of the original position to show you

1 Like

Here is the origin for the handle(The part I want to rotate)


Here is one for the model(just in case you need it)

I think you have a rigging problem or youre cframing it wrong.

The lever seems fine (the stick) however, the handle itself seems to be going to where the start of the stick is.

You should have a rootpart of the model and change the cframe of that so that your handle comes naturally

Maybe weld it if you havent already?

Alternatively just save the cframe of the intended position and just use that instead of angles and positions.

But the model itself isn’t only the stick


It’s the whole thing in the middle
I noticed something for some reason When I use

LeverHandle.Orientation = Vector3.new(120,0,180)

It works fine without any issues but although I got the result I wanted I am really curious on why this happens I don’t wanna just do the solution without knowing why it happens
If you have any idea tell me please

1 Like

The difference in this is that

  • Cframe has position and angle, but in this case, you’re using only orientation in both.
  • you are giving values in x AND z in vector, while in cframe, youre only giving it x and not z.

I think that in the cframe operation you didn’t give it a z.

CFrame is so weird
I tried this while making sure I set x And z

the first attempt failure
the second attempt work as intended
the third attempt failure
the forth attempt works
the last two worked and failed

what???
my game is trolling me
(When it fails the lever remains in the same position and orientation)

local Angle = CFrame.Angles(math.rad(120),0,math.rad(180))
LeverHandle.CFrame = CFrame.new(LeverHandle.Position) * Angle

Yea but x and z works yea? You can convert vector to cframe by using cframe.new and use the vector3 as a parameter. If you are using math.rad, consider removing them to see if it works

I don’t know how to use vector3 as a parameter to adjust the angle all what I see errors because the game thinks this is cframe.lookat but somehow works(with tons of errors)

Do you have any idea on why this doesn’t work?

LeverHandle.CFrame *= CFrame.Angles(math.rad(60),0,0)

Edit: Ik i asked for alot but this is the most confusing issue i have ever had in my entire life(not exaggerating)