What do you want to achieve?
I’m trying to create a working door for my model.
What is the issue?
If the case’s primary part is in a specific orientation, the door will glitch out.
What solutions have you tried so far?
I tried several solutions, looking on the DevForum, etc.
So, I’ve created this door for my model, it’s supposed to open when you press a button located on the case, and close if it’s already open. But there’s an issue, if the case is in a specific orientation, it will work fine. But if it’s any other orientation than that working one, the door will glitch out. Here’s the working orientation:
This will happen if it’s not in that exact orientation, applies to all 3 different orientations:
local TweenService = game:GetService("TweenService")
local button = script.Parent.Button
local model = script.Parent
local originalCFrame = model:GetPrimaryPartCFrame()
local isRotated = false
local function rotateModel()
button.Sound:Play()
isRotated = not isRotated
local rotationCFrame = isRotated and CFrame.Angles(0, math.rad(95), 0) or CFrame.new()
local newPosition = isRotated and originalCFrame.Position + Vector3.new(1, 0, 1) or originalCFrame.Position
local newCFrame = CFrame.new(newPosition) * rotationCFrame
local tweenInfo = TweenInfo.new(
0.5,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0.1
)
local tweenGoal = {}
tweenGoal.CFrame = newCFrame
local tween = TweenService:Create(model.PrimaryPart, tweenInfo, tweenGoal)
tween:Play()
end
button.ClickDetector.MouseClick:Connect(rotateModel)
I’m not usre what the problem was from, but I changed the script up a bit and it seems to be working properly:
local TweenService = game:GetService("TweenService")
local button = script.Parent.Button
local model = script.Parent
local originalCFrame = model.PrimaryPart.CFrame
local isRotated = false
local function rotateModel()
button.Sound:Play()
isRotated = not isRotated
local rotationCFrame = isRotated and originalCFrame * CFrame.Angles(0, math.rad(95), 0) or originalCFrame
local newPosition = isRotated and originalCFrame.Position + Vector3.new(1, 0, 1) or originalCFrame.Position
local tweenInfo = TweenInfo.new(
0.5,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0.1
)
local tweenGoal = {}
tweenGoal.CFrame = rotationCFrame
local tween = TweenService:Create(model.PrimaryPart, tweenInfo, tweenGoal)
tween:Play()
end
button.ClickDetector.MouseClick:Connect(rotateModel)
My guess of what the problem was would be these lines since their the ones I changed:
local originalCFrame = model:GetPrimaryPartCFrame()
local rotationCFrame = isRotated and CFrame.Angles(0, math.rad(95), 0) or CFrame.new()
local newPosition = isRotated and originalCFrame.Position + Vector3.new(1, 0, 1) or originalCFrame.Position
local newCFrame = CFrame.new(newPosition) * rotationCFrame
If this didn’t fix your problem tell me and I’ll try to fix it again!
Could you try changing the PivotOffset of the model so it’s positioned like a hinge on the door? It will rotate from that pivot instead of from the center
To fix this, create a new part that is invisible. Anchor it, and name it “Hinge”. (edit: PLACE IT WHERE THE HINGE WOULD GO ON THE DOOR! WHEREVER YOU WANT IT TO ROTATE FROM!!!)
Unanchor the door and create a weld constraint that is parented to the door. Set the constraint’s “Part0” to the Door, and “Part1” to the hinge.