Hello, I am trying to get a vehicle door to open/close when pressing E on it.

i think i have narrowed it down to the code below which is causing the issue.

		createButton:NewPulse(door.Remote, function(player, wire)
			
			local torso
			if not wire then
				torso = player.Character and player.Character:FindFirstChild("Torso")
			elseif wire then
				torso = wire.PrimaryPart
			end			

			if not (pos.Value == 0) or not torso then
				pos.Value = 1 
			else
				if posConstraint then
					pos.Value = posConstraint
				else
					local p = hingeAnchor.CFrame:pointToObjectSpace(torso.Position)
					if p.Z > 0 then
						pos.Value = 1
					else
						pos.Value = -1
					end	
				end
			end

	 		toggleSwitch()
		end)

Anyone have an idea?

If a player jumps off the driver seat the door will open automatically. if the door is open and a user get on the seat the door will close. This is accomplished by the code below and it works fine.

		pos.Changed:connect(toggleSwitch)
		toggleSwitch()

		local seat = door.VehicleSeat
		if seat then
			seat.Changed:connect(function(property)
				if property == "Occupant" then
					if seat.Occupant then
						pos.Value = 0
					else
						pos.Value = posConstraint or 1
					end					
					toggleSwitch()
				end
			end)
		end
	end
end

the whole code is

local runService = game:GetService("RunService")
local createButton = require(game.ServerScriptService.GameObjects.WireItems.CreateButton)

local doorModule = {}


--------------------------------------------
------------- door setup ---------------

function doorModule.new(doorList)

	for _, door in pairs(doorList) do
		local hingeAnchor = door.Anchor
		local hinge = door.Hinge
		
		local pos = door.Pos
		local posConstraint = door.PosConstraint
		
		local toggleMotor = Instance.new("Motor")
		toggleMotor.Part0 = hingeAnchor
		toggleMotor.Part1 = hinge
		toggleMotor.C0 = CFrame.Angles(-math.pi/2, math.pi, 0) * CFrame.new(hingeAnchor.Size.X / 2, 0, 0)
		toggleMotor.C1 = CFrame.Angles(0, math.pi/2, 0) * CFrame.new(hinge.Size.Z / 2, 0, 0)
		toggleMotor.MaxVelocity = 0.08
		toggleMotor.Parent = toggleMotor.Part0
	
		door.Model:MakeJoints()
	
		local function unanchor(part)
			for _, part in pairs(part:GetConnectedParts()) do
				if part.Anchored and not (part == hingeAnchor) and part.Parent == door.Model then
					part.Anchored = false
					unanchor(part)
				end
			end
		end
		unanchor(hinge)
		
		if not door.Remote:FindFirstChild("ButtonPrompt") then
			print(door.Remote:GetFullName())
		end
		local doorName = door.Remote.ButtonPrompt.Value
	
		local function toggleSwitch()
	
			toggleMotor.DesiredAngle = pos.Value * math.pi/2
			
			if pos.Value == 0 then
				door.Remote.ButtonPrompt.Value = "Open "..string.lower(doorName)
			else
				door.Remote.ButtonPrompt.Value = "Close "..string.lower(doorName)
			end
		end
	
	
		createButton:NewPulse(door.Remote, function(player, wire)
			
			local torso
			if not wire then
				torso = player.Character and player.Character:FindFirstChild("Torso")
			elseif wire then
				torso = wire.PrimaryPart
			end			

			if not (pos.Value == 0) or not torso then
				pos.Value = 1 -- This was 0, this allowed the door to close but not open.
			else
				if posConstraint then
					pos.Value = posConstraint
				else
					local p = hingeAnchor.CFrame:pointToObjectSpace(torso.Position)
					if p.Z > 0 then
						pos.Value = 1
					else
						pos.Value = -1
					end	
				end
			end

	 		toggleSwitch()
		end)
		
		pos.Changed:connect(toggleSwitch)
		toggleSwitch()
		
		local seat = door.VehicleSeat
		if seat then
			seat.Changed:connect(function(property)
				if property == "Occupant" then
					if seat.Occupant then
						pos.Value = 0
					else
						pos.Value = posConstraint or 1
					end					
					toggleSwitch()
				end
			end)
		end
	end
end


return doorModule