Door Mechanics (Open System)

Actually i have fixed my code and optimized your door model! I mean i have deleted all useless parts for me!
DoorTestsFixed2.rbxl (74.2 KB)

(I have reuploaded this file cuz there was my mistake, i have created multiple useless welds)

local TweenService = game:GetService("TweenService")

local doorModel = script.Parent -- Assuming the script is a child of the door model
local mainDoorModel = doorModel.DoorPart -- Model you want to rotate
local proximityPrompt = doorModel.DoorPart.Prox:FindFirstChild("ProximityPrompt")
local proximityPromptBack = doorModel.DoorPart.Prox2:FindFirstChild("ProximityPrompt")
local primaryPart = mainDoorModel.PrimaryPart

local hinge = mainDoorModel.Hinge -- Assuming there's a part named "Hinge" as the hinge point

local DB = false
local isOpen = false

local savedCFrame = mainDoorModel.PrimaryPart.CFrame -- Just to make our life easier

local openAngle = -90 -- Adjust the angle as needed
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)

-- Ensure all decals are welded to the door part
for _, child in ipairs(mainDoorModel:GetChildren()) do
	if child:IsA("BasePart") and child ~= primaryPart then
		local weld = Instance.new("Weld")
		weld.Part0 = primaryPart
		weld.Part1 = child
		weld.C0 = primaryPart.CFrame:Inverse() * child.CFrame
		weld.Parent = primaryPart
	end
end

for _, child in ipairs(doorModel:GetChildren()) do
	if child:IsA("BasePart") and child.Name ~= "Frame" then
		local weld = Instance.new("Weld")
		weld.Part0 = primaryPart
		weld.Part1 = child
		weld.C0 = primaryPart.CFrame:Inverse() * child.CFrame
		weld.Parent = primaryPart
	end
end


local function getOppositeDirection(player)
	local playerPosition = player.Character.PrimaryPart.Position
	local doorPosition = primaryPart.Position
	local doorLookVector = primaryPart.CFrame.LookVector
	local direction = (playerPosition - doorPosition).Unit
	return primaryPart.CFrame.LookVector:Dot(direction) > 0 and -1 or 1
end

local function onProximityPromptTriggered(player)
	if DB then return end
	DB = true
	
	if isOpen then
		isOpen = false
		
		local tween = TweenService:Create(primaryPart, tweenInfo, {CFrame = savedCFrame})
		tween:Play()
		tween.Completed:Wait()
		-- Add logic to close the door after a delay if needed
		
		DB = false
		
		return
		
	end
	
	isOpen = true
	
	local direction = getOppositeDirection(player)

	-- Calculate the target CFrame for the door
	local hingeCFrame = hinge.CFrame
	local rotation = CFrame.Angles(0, math.rad(openAngle) * direction, 0)
	local goalCFrame = hingeCFrame * rotation * hingeCFrame:Inverse() * primaryPart.CFrame

	local tween = TweenService:Create(primaryPart, tweenInfo, {CFrame = goalCFrame})
	tween:Play()
	tween.Completed:Wait()
	-- Add logic to close the door after a delay if needed

	DB = false
end

proximityPrompt.Triggered:Connect(onProximityPromptTriggered)
proximityPromptBack.Triggered:Connect(onProximityPromptTriggered)

Please reply if it works and send your vid :smiley: You can test some tween animations for your door to look smoother

Thanks for your help!!! Trying to make it look a little better (smoother) and adding sounds!

1 Like

I suggest you download my latest uploaded file, because I’ve tweaked your model and my script a bit, but anyway! I’m glad this one works.

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