Part doesn't rotate properly

Hi, so what I am trying to achieve is a door that opens/closes whenever you get near it and press E.
I have a local script on the player and it detects whenever the player hits E, and when he does, he fires a remote event, which then a script inside the door picks up the remote event and rotates the door to open, then waits 4 seconds, and then closes it once again. I made the script as generic as possible so that I can just clone the door and not need to change any of the values.
Here is the script inside the door.

local door = script.Parent
local hinge = door.Hinge
local doorsize = door.Size
local startrot = door.Orientation
local startpos = door.Position
local cooldown = false

game.ReplicatedStorage.InteractE.OnServerEvent:Connect(function(player)
	local humanrootpart = player.Character:FindFirstChild("HumanoidRootPart")
	local playerdistance = (door.Position - humanrootpart.Position).Magnitude
	if playerdistance <= door.BillboardGui.MaxDistance and cooldown == false then
		cooldown = true
		for i = startrot.Y,startrot.Y+90 do
			door.CFrame = CFrame.new(hinge.Position) * CFrame.Angles(startrot.X,math.rad(i),startrot.Z) * CFrame.new(0,0,(doorsize.Z/2))
			wait()
		end
		wait(4)
		for i = startrot.Y+90,startrot.Y,-1 do
			door.CFrame = CFrame.new(hinge.Position) * CFrame.Angles(startrot.X,math.rad(i),startrot.Z) * CFrame.new(0,0,(doorsize.Z/2))
			wait()
		end
		wait(1)
		cooldown = false
	end
end) 

So basically whenever the player is in reach of the door, he can press E and the door will rotate, but this is what happens instead:


The door opens correctly but slightly tilts forwards.
This is how the door’s orientation looks BEFORE opening:

and this is how it looks AFTER opening it:

The Y rotation worked, it added 90 to it but for some reason, the Z orientation got rotated 26.62 degrees and I don’t understand why.
BTW, the hinge is for the door to rotate on it.
Any help would be appreciated. Thanks

2 Likes

What the thing inside the for loop is to set the door inside the hinge, then rotate it and then offset it, and since the door gets placed inside the hinge, half the door is in one side of the hinge and the other one other side, so that’s why the offset is half the door’s lenght.

Any help would be apreciated!

1 Like

Okay, I figured it out.
CFrame.Angles takes in radians, so full rotations, and for it to use degrees, you need to use math.rad(value)
I just had 1 of those angles set to that when it should’ve been the 3 of them.

1 Like