I am working on a vault door that will tween 90 degrees to the right when it opens. As you can see below, it works pretty nicely:
However, once I start rotating the model around, it does not open as nicely or the right way I want it too, as seen here:
How do I fix this? Below is the code:
local old = script.Parent.PrimaryPart.Orientation
local new = old + Vector3.new(0,90,0)
local module = require(workspace.TMmoduleV2)
local info = TweenInfo.new(3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
task.wait(2)
task.spawn(function() module.TweenModuleOrientation(script.Parent,info,new) end)
---here is the tween function in entirety:
local module = {}
module.__index = module
local TS = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local function CalculatePosition(part1,part2)
return CFrame.new(part1.Position):ToObjectSpace(CFrame.new(part2.Position)).Position
end
function module.TweenModuleOrientation(Model,Tweeninfo,oOrinetation)
if typeof(Model) ~= "Instance" then error(Model.." isnt a instance") end
if not Model:IsA("Model") then error(Model.Name.." isnt a model") end
if not Model.PrimaryPart then Model.PrimaryPart = Model:FindFirstChildWhichIsA("BasePart") end
task.spawn(function()
local Primary = Model.PrimaryPart
local AnchorState = Primary.Anchored
local TW = TS:Create(Primary,Tweeninfo,{CFrame = Primary.CFrame * CFrame.Angles(math.rad(oOrinetation.X),math.rad(oOrinetation.Y),math.rad(oOrinetation.Z))})
for _,v in pairs(Model:GetDescendants()) do
if v:IsA("BasePart") and v ~= Primary then
local weld
if not v:FindFirstChild("TweenWeld") then
weld = Instance.new("WeldConstraint")
weld.Part0 = Primary
weld.Part1 = v
weld.Parent = v
weld.Name = "TweenWeld"
else
weld = v:FindFirstChild("TweenWeld")
weld.Enabled = true
end
local anchord = v.Anchored
v.Anchored = false
task.spawn(function()
TW.Completed:Wait()
weld.Enabled = false
v.Anchored = anchord
end)
continue
end
if v:IsA("Weld") or v:IsA("WeldConstraint") or v:IsA("ManualWeld") or v:IsA("Motor6D") then
if v.Name ~= "TweenWeld" then
v.Enabled = false
task.spawn(function()
TW.Completed:Wait()
v.Enabled = true
end)
end
continue
end
task.wait()
end
TW:Play()
TW.Completed:Wait()
Primary.Anchored = AnchorState
end)
return
end