How to optimize door animation without invisible parts?

I want the door to move to a specific position with rotation and then return to its original position after a certain period. I’m already using TweenService, a proximity prompt, and two invisible parts to which it has to move toward to. However, I’m curious about how to achieve this without the need for the invisible parts.

I included some comments within the scripts so it is easier to read what’s going on. Feel free to alter it.

File:
door.rbxm (33.0 KB)

The script:

-- Import the TweenService
local TweenService = game:GetService("TweenService")

-- Get references to objects in the hierarchy
local model = script.Parent
local door = model.Door
local closedDoorPosition = model.ClosedDoorPosition
local openDoorPosition = model.OpenDoorPosition
local prompt = model.Door.ProximityPrompt

-- Variables to track the door state and movement
local isMoving = false -- Used to prevent multiple door movements at once
local waitTime = 2 -- Adjust the wait time before the door closes

-- Function to control the door movement
function MoveDoor()
	print("movedoor function fired")
	-- Check if the door is currently moving
	if isMoving then
		return -- Exit the function if it's already in motion
	end

	isMoving = true -- Mark that the door is currently moving
	prompt.Enabled = false -- Disable the proximity prompt to prevent multiple activations
	
	TweenService:Create(door,TweenInfo.new(0.3),{CFrame = openDoorPosition.CFrame}):Play()
	task.wait(waitTime)
	TweenService:Create(door,TweenInfo.new(0.3),{CFrame = closedDoorPosition.CFrame}):Play()
	isMoving = false -- Mark that the door is no longer moving
	prompt.Enabled = true
end

-- Connect the trigger event of the proximity prompt to the MoveDoor function
prompt.Triggered:Connect(function()
	if not isMoving then
		MoveDoor() -- Call the MoveDoor function when the prompt is triggered (if the door isn't already moving)
	end
end)

Screenshot:

You can try adding a hinge and weld it to the door so you can just rotate your hinge with TweenService

Not sure why you chose to use invisible parts in the first place. You could literally just used CFrames stored as variables, and have them be applied in objectspace relative to the door frame when it’s time to use them.

This is an example:

local doorDefault: CFrame = door.CFrame --CFrame when door is closed
local doorOffset: CFrame = CFrame.new(2, 0, 1)*CFrame.Angles(0, math.rad(-90), 0) --CFrame when door is opened
local doorTweenInfo: TweenInfo = TweenInfo.new(0.3)

--open door
TweenService:Create(door, dorTweenInfo, {CFrame = doorDefault * doorOffset})

Commenting/Adding to @Prototrode’s response, I do understand why you used invisible parts since its simple and requires less coding at the expense of more instances, but you could use CFrames or Vector3s to align the position of the door.

Alternatively, you can use @Kruizbeng’s approach, which I’m not sure if he meant an actual hinge instance or a part named a hinge, you can use a single invisible part, place it where a hinge would go, and set that part as the ‘PrimaryPart’. Once that is done, use :PivotTo() and tween service to rotate your door which will now only need a rotation element instead of position. (I’d recommend this method)

I’m not sure if I explained this well, but if you need more details, I’d be glad to clarify anything.
Good luck!

The invisible parts are good and they should stay. They allow you to specify the movement of the door using Studio draggers. Adjusting CFrames in the code is wasted effort and won’t scale when you resize the door unless you specifically make it check the size of the door. Just store the CFrames of the two parts in variables and delete the parts, that’s all.
(In fact, you can just Destroy the two invisible parts, change nothing else about the code and be a lot better off because the CFrames of the parts don’t change and the parts don’t become unusable when you do that.)

This is somewhat unrelated and Kruizbeng brought it up without elaborating, but you should be tweening a hinge part that the door rotates around. The hinge should be anchored, all other parts of the door should be unanchored and welded to the hinge. The hinge should always stay in the same position, only its rotation should change. If you just tween the entire door’s CFrame, it will unhinge while in the middle of the motion, which looks really funny. Make the door rotate nearly 180° to see what I mean more clearly.
If the door is composed of more than one part, this will also reduce network traffic significantly.

PivotTo doesn’t play well with Tweens because there’s no scriptable Pivot cframe property for Tweens to edit. You’d have to implement a tween yourself (just like before TweenService was introduced) and call PivotTo.

1 Like

I have tried the example, fixed some of the typos, but without success. The door would not move to the desired position and there are no errors found within the output.

Sounds like a great idea, and I have used your method to remove the invisible parts and save the location. However, if I wish to make it a hinge door and use a hinge, the tweening won’t work for it, and I have no idea why. I have even used the following information to make it as good as possible with the hinge:

I’m attaching the file so you can take a look for yourself. Thank you again for your helpful advice.
Door with hingeconstraint.rbxm (36.3 KB)

1 Like

You can create a modified CFrame in a variable, that you call later in your tween creation. If you need help understanding some of the math, feel free to ask.

File:
door.rbxm (32.9 KB)

1 Like

I REMEMBER YOU! Katrist you absolute unit of a person, you always find the correct solutions to my issues. I wish we were still friends haha!

1 Like

Thank you, and sorry for unfriending you, I cleared my friends list a while ago so I probably unfriended you by accident.

I don’t blame you. Since we haven’t talked much lately, I thought I might have bored you. However, I am very thankful for your assistance once again. You are an absolute genius!

I will re-add you if you do not mind. If you wish not to accept, I fully understand. Just know I am very thankful again for your helpful results!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.