How to move a door from one CFrame to another

I am trying to move the door up, however it spins around, how to fix that?

local Model = script.Parent

local StaticPosition = Model:GetPrimaryPartCFrame().Position
local StaticLookVector = Model.PrimaryPart.Orientation

local Positions = {
	up = CFrame.new(
		StaticPosition + Vector3.new(0, 9, 0),
		StaticLookVector
	),
	down = CFrame.new(
		StaticPosition,
		StaticLookVector
	)
}

local Open = Instance.new("BoolValue", Model)
Open.Value = false

local Locked = Instance.new("BoolValue", Model)
Locked.Value = false

local TweenService = game:GetService("TweenService")

local function TweenModel(Group, CF)
	local CFrameValue = Instance.new("CFrameValue")
	CFrameValue.Value = Group:GetPrimaryPartCFrame()
	CFrameValue:GetPropertyChangedSignal("Value"):Connect(function()
		Group:SetPrimaryPartCFrame(CFrame.new(CFrameValue.Value.Position, StaticLookVector))
	end)
	local Tween = TweenService:Create(CFrameValue, TweenInfo.new(1, Enum.EasingStyle.Linear), {Value = CF})
	Tween:Play()
	Tween.Completed:Connect(function()
		CFrameValue:Destroy()
		if Open.Value then
			Open.Value = false
		else
			Open.Value = true
			delay(1, function()
				TweenModel(Model, Positions.down)
			end)
		end
	end)
end

while wait() do
	local detectedHumanoid = false
	for _, v in pairs(workspace:GetChildren()) do
		local HRP = v:FindFirstChild("HumanoidRootPart")
		if HRP and Model.PrimaryPart then
			if (Model.PrimaryPart.Position - HRP.Position).Magnitude <= 8 then
				detectedHumanoid = true
			end
		end
	end
	if detectedHumanoid and not Open.Value then
		TweenModel(Model, Positions.up)
	end
end

Any ideas? I am not good at CFrame's.

1 Like

for changing the CFrame to go up, Instead of doing GetPrimaryPartCFrame().Position, you can just do something like this:

local StaticPosition = Model:GetPrimaryPartCFrame()

StaticPosition + Vector3.new(0,9,0)

Just implement this into your script.

1 Like

Thanks for the tip. However the door still tends to spin around. I fixed it!