Help with a Door Script

This is a script I found in a free model for a door that opens and closes with a command prompt.

I want to make it so that whenever the command prompt is activated, the door’s cancollide will turn false (script.parent) and turn back to true after the door opens/closes.

The problem I’m having is that after the command prompt is activated, the door’s cancollide stays false and does not go back to true and I can’t find a way to do this.

local RunService = game:GetService("RunService")

local ancestorModel = script:FindFirstAncestorWhichIsA("Model")

local promptObject = script.Parent.ProximityPrompt

-- Get the constraint which operates the door
local doorFrame = ancestorModel:WaitForChild("Door_Interior_Frame_A")
local constraint = doorFrame:WaitForChild("HingeConstraint")
local doorClosedConstraint = script.Parent.DoorClosedConstraint

-- Open or close the door based on its current state
local isOpen = script.Parent.isOpen

while true do
	wait(0.5)
	script.Parent.CanCollide = true;
end

promptObject.Triggered:Connect(function(player)
	if isOpen.Value == false then
		local character = player.Character
		local rootPart:Part = character and character:FindFirstChild("HumanoidRootPart")
		if not rootPart then
			return
		end
		
		local direction = math.sign((rootPart.Position - doorFrame.Position):Dot(doorFrame.CFrame.LookVector))
		if direction < 0 then
			constraint.TargetAngle = constraint.LowerAngle
		else
			constraint.TargetAngle = constraint.UpperAngle
		end
	elseif isOpen.Value == true then
		constraint.TargetAngle = 0
	end

	-- Activate the hinge servo
	constraint.AngularSpeed = 2

	-- Disable the prompt while door is opening or closing
	promptObject.Enabled = false
	doorClosedConstraint.Enabled = false

	local connection
	connection = RunService.Stepped:Connect(function(t, dt)
		if math.abs(constraint.CurrentAngle - constraint.TargetAngle) < 0.005 then
			isOpen.Value = not isOpen.Value
			promptObject.Enabled = true
			doorClosedConstraint.Enabled = true
			connection:Disconnect()
		end
	end)
end)

1 Like

Sorry I gave the wrong script, I didn’t mean to add the while true do loop

local RunService = game:GetService("RunService")

local ancestorModel = script:FindFirstAncestorWhichIsA("Model")

local promptObject = script.Parent.ProximityPrompt

-- Get the constraint which operates the door
local doorFrame = ancestorModel:WaitForChild("Door_Interior_Frame_A")
local constraint = doorFrame:WaitForChild("HingeConstraint")
local doorClosedConstraint = script.Parent.DoorClosedConstraint

-- Open or close the door based on its current state
local isOpen = script.Parent.isOpen

promptObject.Triggered:Connect(function(player)
script.Parent.CanCollide = false
	if isOpen.Value == false then
		local character = player.Character
		local rootPart:Part = character and character:FindFirstChild("HumanoidRootPart")
		if not rootPart then
			return
		end
		
		local direction = math.sign((rootPart.Position - doorFrame.Position):Dot(doorFrame.CFrame.LookVector))
		if direction < 0 then
			constraint.TargetAngle = constraint.LowerAngle
		else
			constraint.TargetAngle = constraint.UpperAngle
		end
	elseif isOpen.Value == true then
		constraint.TargetAngle = 0
	end

	-- Activate the hinge servo
	constraint.AngularSpeed = 2

	-- Disable the prompt while door is opening or closing
	promptObject.Enabled = false
	doorClosedConstraint.Enabled = false

	local connection
	connection = RunService.Stepped:Connect(function(t, dt)
		if math.abs(constraint.CurrentAngle - constraint.TargetAngle) < 0.005 then
			isOpen.Value = not isOpen.Value
			promptObject.Enabled = true
			doorClosedConstraint.Enabled = true
			connection:Disconnect()
		end
	end)
end)
1 Like
local RunService = game:GetService("RunService")

local ancestorModel = script:FindFirstAncestorWhichIsA("Model")

local promptObject = script.Parent.ProximityPrompt

-- Get the constraint which operates the door
local doorFrame = ancestorModel:WaitForChild("Door_Interior_Frame_A")
local constraint = doorFrame:WaitForChild("HingeConstraint")
local doorClosedConstraint = script.Parent.DoorClosedConstraint

-- Open or close the door based on its current state
local isOpen = script.Parent.isOpen

promptObject.Triggered:Connect(function(player)
script.Parent.CanCollide = false
	if isOpen.Value == false then
		local character = player.Character
		local rootPart:Part = character and character:FindFirstChild("HumanoidRootPart")
		if not rootPart then
			return
		end
		
		local direction = math.sign((rootPart.Position - doorFrame.Position):Dot(doorFrame.CFrame.LookVector))
		if direction < 0 then
			constraint.TargetAngle = constraint.LowerAngle
		else
			constraint.TargetAngle = constraint.UpperAngle
		end
	elseif isOpen.Value == true then
		constraint.TargetAngle = 0
	end

	-- Activate the hinge servo
	constraint.AngularSpeed = 2

	-- Disable the prompt while door is opening or closing
	promptObject.Enabled = false
	doorClosedConstraint.Enabled = false

    script.Parent.CanCollide = false

	local connection
	connection = RunService.Stepped:Connect(function(t, dt)
		if math.abs(constraint.CurrentAngle - constraint.TargetAngle) < 0.005 then
			isOpen.Value = not isOpen.Value

            script.Parent.CanCollide = true

			promptObject.Enabled = true
			doorClosedConstraint.Enabled = true
			connection:Disconnect()
		end
	end)
end)

altered it a bit, copy and replace the old code to see if this will work for you

2 Likes

Sorry for the late response but it works, thank you!

3 Likes

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