Door mechanic not working correctly!

I have been working on a game close to Apex Legends and the door system I have coded works but it bugs out on some doors.

Video:

Code:

local _Door = {}

function _Door:SetCollisions()	
	--for _, Part in pairs(workspace._THINGS.Doors:GetDescendants()) do
	--	if Part:IsA("BasePart") or Part:IsA("Part") or Part:IsA("Mesh") or Part:IsA("MeshPart") then
	--		game:GetService("PhysicsService"):SetPartCollisionGroup(Part, "Doors")
	--	end
	--end
	return
end

function _Door:DoorFunc(door)	
	local Amount = 5
	local Door = door.PrimaryPart
	local Pivot = Door:GetPivot()
	local Offset = Pivot:Inverse() * Door.CFrame
	local RotDoor = door:GetAttribute('Rot')
	
	if door.Main and door.Main.IsDoorOpened.Value == false then
		--door.Main.Keybind.ProximityPrompt.ActionText = "Close Door"
		door.Main.Keybind.ProximityPrompt.ActionText = ""

		print(RotDoor.."Top")

		repeat 				
			task.wait(math.random(0, 0.1))
			if RotDoor == 0 then
				Pivot *= CFrame.Angles(0,math.rad(Amount), 0) 
				Door.CFrame = Pivot * Offset
			elseif RotDoor == 180 then
				Pivot *= CFrame.Angles(0,math.rad(-Amount), 0) 
				Door.CFrame = Pivot * Offset
			end
			Door.CFrame = Pivot * Offset
		until Door.Orientation.Y == 90

		door.Main.IsDoorOpened.Value = true

	elseif door.Main and door.Main.IsDoorOpened.Value == true then
		--door.Main.Keybind.ProximityPrompt.ActionText = "Open Door"
		door.Main.Keybind.ProximityPrompt.ActionText = ""

		print(RotDoor.."Bottom")

		repeat 
			task.wait(math.random(0, 0.2))
			if RotDoor == 0 then
				Pivot *= CFrame.Angles(0,math.rad(-Amount), 0) 
				Door.CFrame = Pivot * Offset
			elseif RotDoor == 180 then
				Pivot *= CFrame.Angles(0,math.rad(Amount), 0) 
				Door.CFrame = Pivot * Offset
			end
			Door.CFrame = Pivot * Offset
		until Door.Orientation.Y == RotDoor

		door.Main.IsDoorOpened.Value = false
	end
end

return _Door

Rot Meaning:
Rot is the normal rotation of the door

1 Like

From the video it seems the doors that are hinged from the left are the ones giving issue.