Coroutine still running after yielded

Why does the coroutine run still after I yield it

local cor = coroutine.create(function()
workspace.Doors.Door.Button.isopen.Changed:Connect(function(f)
			if script:GetAttribute("State") == 6 then
			if f == false then
				print("yield")
				coroutine.yield()
			end
			end
		end)
			wait(math.random(10, 17))
			
			if script:GetAttribute("State") == 6 then
				workspace.Doors.Door.Button.ClickDetector:SetAttribute("Disabled", true)
				light1.PointLight.Enabled = false
	
				light1b.isopen.Value = false
				light1b.Color = Color3.fromRGB(91, 93, 105)
				
				if workspace.CameraOpen.Value == false then
					workspace.CameraOpen.Changed:Wait()
					workspace.CameraOpen.Changed:Wait()
				
				else
					workspace.CameraOpen.Changed:Wait()
				end
			end
				
	end)

local cor2 = coroutine.create(function()
		workspace.Doors.Door.Button.isopen.Changed:Connect(function(f)
			if script:GetAttribute("State") == 6 then
			if f == true then
				coroutine.yield()
			end
			end
		end)
			wait(math.random(20, 30))
		
--code that's not useful here

			end)
			
			
	end)

the part that resumes the coroutine:

local function manageCoroutines(ope)
	if script:GetAttribute("State") == 6 then
		if ope == false then
			coroutine.resume(cor2)
		
		else
			coroutine.resume(cor)
			
		end
	end
	end

	manageCoroutines(workspace.Doors.Door.Button.isopen.Value)
	
	workspace.Doors.Door.Button.isopen.Changed:Connect(function(f)
		manageCoroutines(f)
	end)

I think the problem has to do with math.random(10, 17)

so, if when you do coroutine.yield() you want it to finish running cor/cor2, then you should use coroutine.close() instead.

local cor 
cor = coroutine.create(function()
    workspace.Doors.Door.Button.isopen.Changed:Connect(function(f)
        if script:GetAttribute("State") == 6 then
            if f == false then
                print("yield")
                coroutine.close(cor)
            end
        end
    end)
    task.wait(math.random(10, 17))
    print("End")
end)

local cor2 
cor2 = coroutine.create(function()
    workspace.Doors.Door.Button.isopen.Changed:Connect(function(f)
        if script:GetAttribute("State") == 6 then
            if f == true then
                print("yield2")
                coroutine.close(cor2)
            end
        end
    end)
    task.wait(math.random(20, 30))
    print("End2")
end)

also, you have to use task.wait instead of wait.

The thing I’m trying to do is have coroutine1 wait to do an event and also have coroutine2 wait to do an event. When the door opens or closes, one coroutine continues to wait to do its event and the other yields until the door reverses its state. When the door reverses its state (opening or closing) the currently running coroutine yields and the yielded coroutine runs and it can go back and forth until one of the two events happen.

I wouldn’t use coroutines for this.