Disclaimer: I'm a new developer and just learned CFrames a few days ago and I wanted to apply it to my game by using it in a door.
I made a door that rotates 90 degrees when opened and -90 degrees when it is closed (toggled twice)
Here is the code:
local door = script.Parent.Door
local hinge = script.Parent.Hinge
local isOpened = false
script.Parent.Knob.ClickDetector.MouseClick:Connect(function(plr)
local offset = door.CFrame * (hinge.CFrame:Inverse())
if isOpened then
hinge.CFrame = hinge.CFrame* CFrame.Angles(0,math.rad(-90),0)
door.CFrame = hinge.CFrame *offset
else
hinge.CFrame = hinge.CFrame* CFrame.Angles(0,math.rad(90),0)
door.CFrame = hinge.CFrame * offset
end
isOpened = not isOpened
end)
Here is what happens in the game https://gyazo.com/608bc1f0f2d9432e3c262b877dea2ba8
I don't get why it works on the first try but does not apply the offset and instead, it just rotates not relative to the hinge after the first try.
