CFrame Adjusting Orientation

Hey there,

I am currently working on a game. It has a lot of doors that rely on the Tweening Service and CFrame, So every time a door animation plays for a certain door, when it was built the Door and Other objects weren’t in the same Orientation as they are now.

So basically what is happening is that when playing a Tween Animation the Door will reset to it’s original orientation thus making the door stick out of the wall.

Is there anyway I can stop this from happening or will I have to entirely recreate the door?

The Script

local ProximityPrompt = script.Parent

local Door = script.Parent.Parent.Parent.Door

local TweenService = game:GetService("TweenService")

local tweeningInfo = TweenInfo.new(
	5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0

)

local tweenOpen = {CFrame =CFrame.new(-591.214, 43.204, -102.568)}
local tweenClose = {CFrame = CFrame.new(-591.214, 36.504, -102.568)}


local OpenTween = TweenService:Create(Door, tweeningInfo, tweenOpen)
local CloseTween = TweenService:Create(Door, tweeningInfo, tweenClose)


ProximityPrompt.Triggered:Connect(function()
	script.Parent.Light.BrickColor = BrickColor.new("Lime green")
	OpenTween:Play()
	ProximityPrompt.MaxActivationDistance = 0
	wait(10)
	script.Parent.Light.BrickColor = BrickColor.new("Bright red")
	CloseTween:Play()
	wait(5)
	ProximityPrompt.MaxActivationDistance = 10
end)

Thank you for the assistance! :slight_smile:

Sorry if this doesn’t make much sense.

Just set the door cframe back to original once it finishes playing

The door already resets to the original position.

The doors has turned 90 degrees by the time it opens then remains that way.

If we assume that the door is initially closed, we only need to find the offset (with :ToObjectSpace) and add it to the initial position of the current door.

local position1 = CFrame.new(-591.214, 43.204, -102.568) -- open
local position2 = CFrame.new(-591.214, 36.504, -102.568) -- closed. original initial position

local tweenOpen = {CFrame = Door.CFrame * position2:ToObjectSpace(position1)} -- position2:ToObjectSpace(position1) is the offset
local tweenClose = {CFrame = Door.CFrame}

I have tried your solution and got only 1 error:

Workspace.Door.KeycardReader.ProximityPrompt.Script:26: attempt to call a nil value

Try using Go to parts:

{ CFrame = door.GoTo.CFrame }

It will prevent orientation issues as the go to part is also rotated if the door model is rotated

This doesn’t work either. Also are you sure that’s a proper variable, I am not judging you or anything it’s just that I have never seen anyone use that before.

I also got this error in the output:
GoTo is not a valid member of UnionOperation "Workspace.Door.Door"

1 Like

What he means is you create invisible target parts inside the door part already pre-positioned in studio so then instead of doing

local tweenOpen = {CFrame =CFrame.new(-591.214, 43.204, -102.568)}

You can just do

local tweenOpen = {CFrame = Door.OpenTarget.CFrame}

So I would have to do something like this:

local ProximityPrompt = script.Parent
local openTarget = game.Workspace.Target -- Change Here

local Door = script.Parent.Parent.Parent.Door

local TweenService = game:GetService("TweenService")

local tweeningInfo = TweenInfo.new(
	5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0

)

local openTween = {CFrame = Door.openTarget.CFrame} -- Change Here
local closeTween = CFrame.new(-591.214, 36.504, -102.568)

local tweenOpen = {Door, tweeningInfo, openTween} 
local tweenClose = {Door, tweeningInfo, closeTween}


ProximityPrompt.Triggered:Connect(function()
	script.Parent.Light.BrickColor = BrickColor.new("Lime green")
	tweenOpen:Play()
	ProximityPrompt.MaxActivationDistance = 0
	wait(10)
	script.Parent.Light.BrickColor = BrickColor.new("Bright red")
	tweenClose:Play()
	wait(5)
	ProximityPrompt.MaxActivationDistance = 10
end)

Not quite

local ProximityPrompt = script.Parent
local openTarget = game.Workspace.Target -- Change Here

local Door = script.Parent.Parent.Parent.Door
local originalCFrame = Door.CFrame

local TweenService = game:GetService("TweenService")

local tweeningInfo = TweenInfo.new(
	5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0

)

local openTween = {CFrame = openTarget.CFrame}
local closeTween = {CFrame = originalCFrame}

local tweenOpen = {Door, tweeningInfo, openTween} 
local tweenClose = {Door, tweeningInfo, closeTween}

Alright I tried your method and got this error:

Workspace.Door.KeycardReader.ProximityPrompt.OpenClose:28: attempt to call a nil value

Which is weird because Line 28 is not a Nil value

Current Script:

local ProximityPrompt = script.Parent
local openTarget = game.Workspace.Target -- Change Here

local Door = script.Parent.Parent.Parent.Door
local originalCFrame = Door.CFrame -- Also, this would be set once the server begins and kept that way, correct?

local TweenService = game:GetService("TweenService")

local tweeningInfo = TweenInfo.new(
	5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0

)

local openTween = {CFrame = openTarget.CFrame}
local closeTween = {CFrame = originalCFrame}

local tweenOpen = {Door, tweeningInfo, openTween} 
local tweenClose = {Door, tweeningInfo, closeTween}


ProximityPrompt.Triggered:Connect(function()
	script.Parent.Light.BrickColor = BrickColor.new("Lime green")
	tweenOpen:Play() -- Line 28
	ProximityPrompt.MaxActivationDistance = 0
	wait(10)
	script.Parent.Light.BrickColor = BrickColor.new("Bright red")
	tweenClose:Play()
	wait(5)
	ProximityPrompt.MaxActivationDistance = 10
end)

To play the tween you actually have to do

local TweenService = game:GetService("TweenService")
local ProximityPrompt = script.Parent
local openTarget = game.Workspace.Target -- Change Here

local Door = script.Parent.Parent.Parent.Door
local originalCFrame = Door.CFrame

local TweenService = game:GetService("TweenService")

local tweeningInfo = TweenInfo.new(
	5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0

)

local openTween = {CFrame = openTarget.CFrame}
local closeTween = {CFrame = originalCFrame}

local tweenOpen = TweenService:Create(Door, tweeningInfo, openTween) 
local tweenClose = TweenService:Create(Door, tweeningInfo, closeTween)

Wow, oops.

I forgot to add the TweenService:Create.

Alright,

The script is working properly, but the door still turns 90 degrees then reset upon going back down.

Make sure the target part (game.Workspace.Target) is aligned properly. You should just duplicate the Door part and use that as the target part so you know the orientations are already correct

1 Like

Ah, thank you so much.

I have had the issue previously and could not find any solutions and I have had to restart my entire developing process in my game over. It had only just recently came to me to ask the DevForum.

So thank you and to everyone else who tried helping, it is greatly appreciated! :grin: