Sliding Doors tween breaks after changing the model orientation

Greetings everyone
So I have made this sliding door system for one of my projects.
The problem I have found its the tween slices the doors in the wrong way once the rotation/orientation is changed.

I have tried fixing the problem with the devforum solutions, but It keep refusing to tween the doors properly. I would be glad if anyone has a solution for this because I ran out of ideas.

Video: Sliding doors issue - Album on Imgur
Code:

--// SETTINGS

local OpenCloseTime = 3.3

--// VARIABLES

local Tweenservice = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SecurityRemote = ReplicatedStorage:WaitForChild("SecurityDoor")

local Status = script.Parent:WaitForChild("Status")
local Door1, Door2 = script.Parent:WaitForChild("Door1"), script.Parent:WaitForChild("Door2")
local KeyHole = script.Parent:WaitForChild("Keyhole")

local IsBusy = script.Parent.IsBusy

local GateSound = Door1.Hinge:WaitForChild("GateSFX")

--// POSITIONS

local Door1Rotation = Door1.PrimaryPart.CFrame - Door1.PrimaryPart.CFrame.Position
local Door2Rotation = Door2.PrimaryPart.CFrame - Door2.PrimaryPart.CFrame.Position

local CFrameValue1 = Instance.new("CFrameValue", Door1)
local CFrameValue2 = Instance.new("CFrameValue", Door2)

CFrameValue1.Name = Door1.Name .. "'s CFrame"
CFrameValue2.Name = Door2.Name .. "'s CFrame"

CFrameValue1.Value = Door1:GetPivot()
CFrameValue2.Value = Door2:GetPivot()

local Door1OpenPos = Door1.PrimaryPart.CFrame * Door1Rotation + Vector3.new(0,0,-5)
local Door2OpenPos = Door2.PrimaryPart.CFrame * Door2Rotation + Vector3.new(0,0,5)

local Door1ClosePos = Door1.PrimaryPart.CFrame * Door1Rotation
local Door2ClosePos = Door2.PrimaryPart.CFrame * Door2Rotation

--// MAIN

CFrameValue1.Changed:Connect(function(Value)
	Door1:PivotTo(Value)
end)

CFrameValue2.Changed:Connect(function(Value)
	Door2:PivotTo(Value)
end)

Status.Changed:Connect(function()
	if Status.Value == "Open" then
		if IsBusy.Value == false then
			IsBusy.Value = true
			GateSound:Play()
			KeyHole.Status.BrickColor = BrickColor.new("Black")
			local Tween1 = Tweenservice:Create(CFrameValue1, TweenInfo.new(OpenCloseTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Value = Door1OpenPos})
			local Tween2 = Tweenservice:Create(CFrameValue2, TweenInfo.new(OpenCloseTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Value = Door2OpenPos})
			Tween1:Play()
			Tween2:Play()
			Tween1.Completed:Wait()
			wait(2)
			IsBusy.Value = false
			KeyHole.Status.BrickColor = BrickColor.new("Lime green")
	    end
	end
	if Status.Value == "Close" then  
		if IsBusy.Value == false then
			IsBusy.Value = true
			GateSound:Play()
			KeyHole.Status.BrickColor = BrickColor.new("Black")
			local Tween1 = Tweenservice:Create(CFrameValue1, TweenInfo.new(OpenCloseTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Value = Door1ClosePos})
			local Tween2 = Tweenservice:Create(CFrameValue2, TweenInfo.new(OpenCloseTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Value = Door2ClosePos})
			Tween1:Play()
			Tween2:Play()
			Tween1.Completed:Wait()
			wait(2)
			IsBusy.Value = false
			KeyHole.Status.BrickColor = BrickColor.new("Persimmon")
		end
	end
end)
1 Like

I would use CFrame’s normal vectors such as LookVector, RightVector and UpVector with their negated versions. What you can do with these is to add their value to their original position and multiply it to how far you want the door to go, for example; if the RightVector is Vector3.new(1, 0, 0), the door’s position is Vector3.new(5, 5, 5) and we want the door to go to its right 5 studs we multiply that vector (Vector3.new(5, 0, 0)), then we add it to the door’s position and we get Vector3.new(10, 5, 5). At last we convert it to a CFrame and add its rotation since we’re using a CFrameValue.

local function Tween()
	local Door1Pivot = Door1:GetPivot()
	local Door2Pivot = Door2:GetPivot()
	
	local Tween1 = TweenService:Create(CFValue1, TweenInfo.new(3.3), {Value = CFrame.new(Door1Pivot.Position + -(Door1Pivot.RightVector * 5)) * CFrame.Angles(Door1Pivot:ToEulerAnglesXYZ())})
	local Tween2 = TweenService:Create(CFValue2, TweenInfo.new(3.3), {Value = CFrame.new(Door2Pivot.Position + (Door2Pivot.RightVector * 5)) * CFrame.Angles(Door2Pivot:ToEulerAnglesXYZ())})
	
	Tween1:Play()
	Tween2:Play()
	Tween1.Completed:Wait()
	--etc
end

1 Like

Thank you so much, you have saved me! :bowing_man: